我有一份清单
List<String> names = new ArrayList<String>();
names.add("One");
names.add("Two");
names.add("Three");
names.add("Six");
names.add("Four");
names.add("Five");
Run Code Online (Sandbox Code Playgroud)
我想要这个列表排序,以便Six首先出现,然后它应该按字母顺序排列.
names.sort((x, y) -> {
if (x.startsWith("S"))
return -1;
if (y.startsWith("S"))
return 1;
return x.compareTo(y);
});
Run Code Online (Sandbox Code Playgroud)
这很好用.为什么它不适用于唯一的x.startswith("S")条件.为什么我需要添加条件y.startsWith("S")
我试图用Ruby对这个文本进行排序,但它不起作用.
1,fgff
12,seffdd
2,dssffs
Run Code Online (Sandbox Code Playgroud)
使用:
cat sorting | ruby -ne 'BEGIN{ $x=[]}; $x << $_.chomp.to_s; END { puts $x.sort }'
Run Code Online (Sandbox Code Playgroud)
我得到这个结果:
1,fgff
12,seffdd
2,dssffs
Run Code Online (Sandbox Code Playgroud)
我期待这个
1,fgff
2,dssffs
12,seffdd
Run Code Online (Sandbox Code Playgroud)
我如何修复我的代码?
我有两个java.util.List,例如如下所示:
List<MyObject> myObjects = ...
MyObject myObject1 = ...
MyObject myObject2 = ...
MyObject myObject3 = ...
MyObject myObject4 = ...
MyObject myObject5 = ...
myObjects.add(myObjet1);
...
Run Code Online (Sandbox Code Playgroud)
第二个List看起来像这样:
List<MyObject> otherListObjects = ...
MyObject myObject1 = ...
MyObject myObject5 = ...
Run Code Online (Sandbox Code Playgroud)
现在我的目标是有一个列表,其中myObject1和myObject5位于前两个位置,而不是其他位置.在Java 8中是否有简单的可能性?
这是我的字典列表:
l = [{'a': 2, 'c': 1, 'b': 3},
{'a': 2, 'c': 3, 'b': 1},
{'a': 1, 'c': 2, 'b': 3},
{'a': 1, 'c': 3, 'b': 2},
{'a': 2, 'c': 5, 'b': 3}]
Run Code Online (Sandbox Code Playgroud)
现在,我想按用户提供的键和顺序对列表进行排序。例如:
keys = ['a', 'c', 'b']
orders = [1, -1, 1]
Run Code Online (Sandbox Code Playgroud)
我试图在sort()方法中使用lambda,但是它以一种奇怪的方式失败了:
>>> l.sort(key=lambda x: (order * x[key] for (key, order) in zip(keys, orders)))
>>> l
[{'a': 2, 'c': 5, 'b': 3},
{'a': 1, 'c': 3, 'b': 2},
{'a': 1, 'c': 2, 'b': 3},
{'a': …Run Code Online (Sandbox Code Playgroud) let first = { 2, 4, 5, 9 };
let second = { 2, 8, 15, 53, 4 }
//removed = 5, 9
//added = 8, 15, 53
Run Code Online (Sandbox Code Playgroud)
确定添加或删除内容的最简单的解决方案是什么?
我有一个字符串列表.我只想对符合特定条件的值进行排序.考虑这个清单
['foo','bar','testa','python','java','abc']
Run Code Online (Sandbox Code Playgroud)
我只想用其中的值对值进行排序a.结果应该是这样的
['foo','abc','bar','python','java','testa']
Run Code Online (Sandbox Code Playgroud)
具有的元素a将适当地改变位置,但其他元素保留其原始位置.
我完全不知道如何实现这一点,所以我希望其他人这样做.有人能告诉我怎么做吗?
这是我的问题:我有一个整数列表:7,0,2
如果我使用Collections.sort(list)结果排序列表是:0,2,7
但我想排序列表排除0排序过程,所以输出如下所示:2,0,7.
可能吗?
提前致谢.
编辑:我忘了提到我的3种可能的情况:1)列表只包含一个"0"和两个数字2)列表包含两个"0"和一个数字3)列表包含三个"0"而没有数字
我在画布中引入了一组点: 我的画布带有一组点
我必须将此算法应用于: Algo NoObtuse和此算法生成的图形示例
我的问题是从最右边的点开始,以逆时针顺序找到以下点(算法中的第2点).
那么,每次从一个点开始,我们如何才能在这个方向找到以下几点?
编辑: - > Blindman67的代码结果
//First points (before sort and anti-clockwise)
//(6) [Point, Point, Point, Point, Point, Point]
0: Point {x: 458, y: 249, col: "red"}
1: Point {x: 333, y: 40, col: "red"}
2: Point {x: 138, y: 111, col: "red"}
3: Point {x: 336, y: 209, col: "red"}
4: Point {x: 237, y: 251, col: "red"}
5: Point {x: 60, y: 351, col: "red"}
//Points after sort and anti-clockwise
//(6) [Point, …Run Code Online (Sandbox Code Playgroud) 我有一个列表,列表中的每个元素都是一个字符串,其中包含特定格式的日期和整数:yyyyMMdd_number.
List<string> listStr = new List<string> { "20170822_10", "20170821_1", "20170823_4", "20170821_10", "20170822_11", "20170822_5",
"20170822_2", "20170821_3", "20170823_6", "20170823_21", "20170823_20", "20170823_2"};
Run Code Online (Sandbox Code Playgroud)
使用方法时 listStr.Sort();
结果如下:
20170821_1 20170821_10 20170821_3 20170822_10 20170822_11 20170822_2 20170822_5 20170823_2 20170823_20 20170823_21 20170823_4 20170823_6
预期产出:
20170821_1 20170821_3 20170821_10 20170822_2 20170822_5 20170822_10 20170822_11 20170823_2 20170823_4 20170823_6 20170823_20 20170823_21
方式:我认为每个字符串(day_number)将用下划线分割,然后按数字进行比较和排序.但在这种情况下,请建议我使用LINQ解决方案或更好的方法进行排序.
我这样做是因为我学习编程的网站,我根本无法解决这个问题.
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main ()
{
int n;
string thename;
vector <pair<string,int>> name;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> thename;
name.push_back(make_pair(thename, thename.length()));
}
sort(name.begin(), name.end());
sort(name.begin(), name.end(),
[](const pair<string, int>& lhs, const pair<string, int>& rhs) {
return lhs.second < rhs.second; } );
for (int i = 0; i < n; i++)
{
cout << name[i].first<< endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我的老人建议我不要使用lambda排序,因为我自己仍然不太了解它.但我仍会接受任何lambda排序的答案.谁能帮我?
sorting ×10
java ×3
algorithm ×2
list ×2
python ×2
c# ×1
c++ ×1
canvas ×1
collections ×1
comparator ×1
integer ×1
java-8 ×1
javascript ×1
lambda ×1
linq ×1
python-3.x ×1
ruby ×1
string ×1
swift ×1