英文:
Java sort string array using Array.sort() like python's sorted with key=lambda x:
问题
我偶尔学习Java。作为一个有Python背景的人,我想知道在Java中是否存在类似于Python中的sorted(iterable, key=function)
的功能。
例如,在Python中,我可以按照元素特定的字符对列表进行排序,就像这样:
a_list = ['bob', 'kate', 'jaguar', 'mazda', 'honda', 'civic', 'grasshopper']
s = sorted(a_list) # 首先按升序对所有元素进行排序
print(s)
sorted(s, key=lambda x: x[1]) # 按每个元素的第二个字符进行排序
因此,a_list
首先按升序排序,然后再按每个元素的第二个字符排序。
我的问题是,如果我想在Java中按特定字符按升序对元素进行排序,该如何实现呢?
以下是我编写的Java代码:
import java.util.Arrays;
public class sort_list {
public static void main(String[] args) {
String[] a_list = {"bob", "kate", "jaguar", "mazda", "honda", "civic", "grasshopper"};
Arrays.sort(a_list);
System.out.println(Arrays.toString(a_list));
}
}
结果如下:
[bob, civic, grasshopper, honda, jaguar, kate, mazda]
在这里,我只实现了按升序对数组进行排序。我希望Java数组的结果与Python列表的结果相同。
我对Java还不太熟悉,所以非常感谢任何建议。
提前感谢您的帮助。
英文:
I'm occasionally learning Java. As a person from python background, I'd like to know whether there exists something like sorted(iterable, key=function)
of python in java.
For exmaple, in python I can sort a list ordered by an element's specific character, such as
>>> a_list = ['bob', 'kate', 'jaguar', 'mazda', 'honda', 'civic', 'grasshopper']
>>> s=sorted(a_list) # sort all elements in ascending order first
>>> s
['bob', 'civic', 'grasshopper', 'honda', 'jaguar', 'kate', 'mazda']
>>> sorted(s, key=lambda x: x[1]) # sort by the second character of each element
['jaguar', 'kate', 'mazda', 'civic', 'bob', 'honda', 'grasshopper']
So a_list
is sorted by ascending order first, and then by each element's 1 indexed(second) character.
My question is, if I want to sort elements by specific character in ascending order in Java, how can I achieve that?
Below is the Java code that I wrote:
import java.util.Arrays;
public class sort_list {
public static void main(String[] args)
{
String [] a_list = {"bob", "kate", "jaguar", "mazda", "honda", "civic", "grasshopper"};
Arrays.sort(a_list);
System.out.println(Arrays.toString(a_list));}
}
}
and the result is like this:
[bob, civic, grasshopper, honda, jaguar, kate, mazda]
Here I only achieved sorting the array in ascending order. I want the java array as the same as the python list result.
Java is new to me, so any suggestion would be highly appreciated.
Thank you in advance.
答案1
得分: 2
使用自定义比较器来比较两个字符串。
Arrays.sort(a_list, Comparator.comparing(s -> s.charAt(1)));
这会通过字符串的第二个字符来比较两个字符串。
这将导致:
[kate, jaguar, mazda, civic, bob, honda, grasshopper]
我注意到你的输出中 jaguar
和 kate
被调换了位置。我不确定 Python 如何对待两个相等的字符串。Arrays.sort
使用了稳定排序。
这种排序被保证是稳定的:相等的元素在排序后不会被重新排序。
英文:
Use a custom comparator to compare two strings.
Arrays.sort(a_list, Comparator.comparing(s -> s.charAt(1)));
This compares two strings by the string's second character.
This will result in
[kate, jaguar, mazda, civic, bob, honda, grasshopper]
I see that jaguar
and kate
are switched in your output. I'm not sure how Python orders two String that are equal. The Arrays.sort
does stable sort.
> This sort is guaranteed to be <i>stable</i>: equal elements will
not be reordered as a result of the sort.
答案2
得分: 1
你可以将一个 lambda 函数提供给 Arrays.sort
方法。对于你的示例,你可以使用:
Arrays.sort(a_list, (String a, String b) -> a.charAt(1) - b.charAt(1));
假设你已经对数组进行了字母表排序(使用 Arrays.sort(a_list)
),这将为你提供所需的结果:
['jaguar', 'kate', 'mazda', 'civic', 'bob', 'honda', 'grasshopper']
英文:
You can supply a lambda function to Arrays.sort
. For your example, you could use:
Arrays.sort(a_list, (String a, String b) -> a.charAt(1) - b.charAt(1));
Assuming you have first sorted the array alphabetically (using Arrays.sort(a_list)
) this will give you your desired result of:
['jaguar', 'kate', 'mazda', 'civic', 'bob', 'honda', 'grasshopper']
答案3
得分: 1
可以使用Comparator.comparing
来对列表进行排序:
Arrays.sort(a_list, Comparator.comparing(e -> e.charAt(1)));
如果你想要使用Java Stream API来对列表进行排序并收集到新的列表中:
String[] listSorted = Arrays.stream(a_list)
.sorted(Comparator.comparing(s -> s.charAt(1)))
.toArray(String[]::new);
英文:
You can use Comparator.comparing
to sort the list
Arrays.sort(a_list, Comparator.comparing(e -> e.charAt(1)));
And if you want to sort and collect in the new list using Java Stream API
String [] listSorted = Arrays.stream(a_list)
.sorted(Comparator.comparing(s -> s.charAt(1)))
.toArray(String[]::new);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论