简体   繁体   English

如何在每个ArrayList中按某个值有效地对ArrayList的ArrayList进行排序?

[英]How can I efficiently sort an ArrayList of ArrayLists by a certain value in each ArrayList?

I have an ArrayList used to hold ArrayLists of the following form... 我有一个ArrayList用于保存以下形式的ArrayList ...

[sally, carp, md]
[jim, smith, ny]
[frank, franklin, ca]

I wish to sort the outer ArrayList by the third parameter (state) in the inner ArrayList. 我希望通过内部ArrayList中的第三个参数(状态)对外部ArrayList进行排序。 The result of sorting would be as follows... 排序结果如下...

[frank, franklin, ca]
[sally, carp, md]
[jim, smith, ny]

Is there an efficient way to do this? 有一种有效的方法可以做到这一点吗?

I would not do this. 我不会这样做。

A better idea might be to create an object that encapsulates first name, last name, and state and have a List of those. 一个更好的主意可能是创建一个封装名字,姓氏和状态并具有List的对象。 It's easy with a Comparator once you do that. 完成后,使用Comparator很容易。

Efficiency will be governed by the Big-Oh behavior of your sorting algorithm . 效率将取决于您的排序算法Big-Oh行为

Your best bet is not to write your own sorter. 最好的选择是不要编写自己的分类器。 Use Collections.sort() . 使用Collections.sort()

https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

Yes, it's quite easy with Java8. 是的,使用Java8非常容易。 Given the list: 给定列表:

List<List<String>> list; //let's assume it's initialized

you can sort the outer list by the third element of the inner list with: 您可以使用以下内容按内部列表的第三个元素对外部列表进行排序:

list.sort(Comparator.comparing(l -> l.get(2)));

But @duffymo is right, you probably want to create a proper abstraction on your domain model. 但是@duffymo是正确的,您可能想在域模型上创建适当的抽象。

Assuming you're using an arraylist of arraylists of strings: 假设您使用的是字符串数组列表的数组列表:

arrayList.sort((al1, al2) -> al1[2].compareTo(al2[2]));

This creates a lambda function which accesses third elements to compare. 这将创建一个lambda函数,该函数访问第三个元素进行比较。

However, as @duffymo says, you should probably be using an arraylist of a custom class rather than an arraylist of arraylists. 但是,正如@duffymo所说,您可能应该使用自定义类的数组列表,而不是数组列表的数组列表。 You'd still have to provide a sorting key for the class, though, and you'd write an effectively equivalent lambda function for your class in order to sort it. 但是,您仍然必须为该类提供一个排序键,并且您将为该类编写一个等效的lambda函数,以便对其进行排序。

Edit: To comment on complexity. 编辑:评论复杂性。

Also, @duffymo, I would choose a string sorting algorithm for critical code based on the class of strings I was sorting. 另外,@ duffymo,我会根据要排序的字符串类为关键代码选择字符串排序算法。 Different collections respond differently to sorting algorithms. 不同的集合对排序算法的响应不同。 If you choose a non-comparison algorithm such as Burstsort , Bucket sort and Radix sort (both LSD and MSD implementations), your complexity can be linear with size by taking advantage of the structure of strings. 如果您选择非比较算法,例如Burstsort ,Bucket排序和Radix排序(LSD和MSD两种实现),那么利用字符串的结构,复杂度就可以随大小线性变化。 Any default generic sorting algorithm uses comparisons (such as timsort in Python), which can be proven to take at minimum O(nlog(n)) time. 任何默认的通用排序算法都使用比较(例如Python中的timsort),可以证明这种比较花费最少的 O(nlog(n))时间。 Still, nlog(n) grows decently slowly. 但是,nlog(n)的增长还是相当缓慢的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM