按属性对自定义对象的 ArrayList 进行排序
我读到过有关使用比较器对 ArrayList 进行排序的内容,但在所有示例中,人们都使用了 compareTo
,根据一些研究,它是字符串的一种方法。
我想按自定义对象的属性之一对 ArrayList 进行排序:日期对象 (getStartDay()
)。通常我通过 item1.getStartDate().before(item2.getStartDate())
来比较它们,所以我想知道我是否可以写这样的东西:
public class CustomComparator {
public boolean compare(Object object1, Object object2) {
return object1.getStartDate().before(object2.getStartDate());
}
}
public class RandomName {
...
Collections.sort(Database.arrayList, new CustomComparator);
...
}
I read about sorting ArrayLists using a Comparator but in all of the examples people used compareTo
which according to some research is a method for Strings.
I wanted to sort an ArrayList of custom objects by one of their properties: a Date object
(getStartDay()
). Normally I compare them by item1.getStartDate().before(item2.getStartDate())
so I was wondering whether I could write something like:
public class CustomComparator {
public boolean compare(Object object1, Object object2) {
return object1.getStartDate().before(object2.getStartDate());
}
}
public class RandomName {
...
Collections.sort(Database.arrayList, new CustomComparator);
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(29)
由于
Date
实现 < a href="//docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html" rel="noreferrer">Comparable
,它有一个compareTo
方法就像String
一样。所以你的自定义
Comparator
可能如下所示:compare()
方法必须返回int
,因此您不能像计划那样直接返回boolean
反正。您的排序代码将与您所写的一样:
如果您不需要重用比较器,则编写所有这些内容的一种稍短的方法是将其编写为内联匿名类:
因为 java-8
您现在可以通过以下方式以较短的形式编写最后一个示例:使用 lambda 表达式 作为
Comparator
:并且
List
有一个sort(Comparator)
方法,因此您可以进一步缩短它:这是一个常见的习惯用法,有 用于生成
Comparator 的内置方法
对于具有
Comparable
键的类:所有这些都是等效的形式。
Since
Date
implementsComparable
, it has acompareTo
method just likeString
does.So your custom
Comparator
could look like this:The
compare()
method must return anint
, so you couldn't directly return aboolean
like you were planning to anyway.Your sorting code would be just about like you wrote:
A slightly shorter way to write all this, if you don't need to reuse your comparator, is to write it as an inline anonymous class:
Since java-8
You can now write the last example in a shorter form by using a lambda expression for the
Comparator
:And
List
has asort(Comparator)
method, so you can shorten this even further:This is such a common idiom that there's a built-in method to generate a
Comparator
for a class with aComparable
key:All of these are equivalent forms.
具有自然排序顺序的类(例如 Number 类)应该实现 Comparable 接口,而没有自然排序顺序的类(例如 Chair 类)应该提供一个 Comparator(或匿名 Comparator)班级)。
两个例子:
用法:
Classes that has a natural sort order (a class Number, as an example) should implement the Comparable interface, whilst classes that has no natural sort order (a class Chair, as an example) should be provided with a Comparator (or an anonymous Comparator class).
Two examples:
Usage:
要对 ArrayList 进行排序,您可以使用以下代码片段:
For sorting an
ArrayList
you could use the following code snippet:JAVA 8 lambda 表达式
OR
JAVA 8 lambda expression
OR
是的,你可以。有两个比较项目的选项,可比较 接口和 Comparator 接口。
这两个接口都允许不同的行为。 Comparable 允许您使对象表现得像刚刚描述的 String(事实上,String 实现了 Comparable)。第二个是比较器,允许您执行您要求执行的操作。您可以这样做:
这将导致 Collections.sort 方法使用您的比较器作为其排序机制。如果 ArrayList 中的对象实现可比较,您可以执行如下操作:
Collections 类包含许多有用的常用工具。
Yes, you can. There are two options with comparing items, the Comparable interface, and the Comparator interface.
Both of these interfaces allow for different behavior. Comparable allows you to make the object act like you just described Strings (in fact, String implements Comparable). The second, Comparator, allows you to do what you are asking to do. You would do it like this:
That will cause the Collections.sort method to use your comparator for it's sorting mechanism. If the objects in the ArrayList implement comparable, you can instead do something like this:
The Collections class contains a number of these useful, common tools.
使用 Java 8,您可以为比较器使用方法引用:
With Java 8 you can use a method reference for your comparator:
由于技术每天都在出现,答案也会随着时间而改变。我看了一下 LambdaJ,看起来很有趣。
您可以尝试使用LambdaJ解决这些任务。您可以在这里找到它:http://code.google.com/p/lambdaj/
这里有一个例子:
Sort Iterative
Sort with lambda
当然,这种美感会影响性能(平均 2 次),但是你能找到一个更具可读性的代码?
Since technologies appear everyday, the answer will change in the time. I took a look at LambdaJ and seems very interesting.
You can try solving these tasks with LambdaJ. You can find it here: http://code.google.com/p/lambdaj/
Here you have an example:
Sort Iterative
Sort with lambda
Of course, having this kind of beauty impacts in the performance (an average of 2 times), but can you find a more readable code?
功能&方法参考
Collections.sort
方法可以对List
使用Comparator
你通过了。该Comparator
可以使用Comparator.comparing
方法,您可以在其中传递 方法引用 作为必要的函数
。幸运的是,实际的代码比这个描述更简单、更短。对于 Java 8:
或者
另一种方法是
Function & method reference
The
Collections.sort
method can sort aList
using aComparator
you pass. ThatComparator
can be implemented using theComparator.comparing
method where you can pass a method reference as the necessaryFunction
. Fortunately, the actual code is much simpler and shorter than this description.For Java 8:
or
Another way is
使用 JAVA 8 的最佳简单方法是英语字母排序
类实现
排序
如果您想对包含非英语字符的字母表进行排序,您可以使用 Locale... 下面的代码使用土耳其语字符排序...
类实现
排序
Best easy way with JAVA 8 is for English Alphabetic sort
Class Implementation
Sort
If you want to sort for alphabet that contains non English characters you can use Locale... Below code use Turkish character sort...
Class Implementation
Sort
您可以使用 java 8 进行排序
You can Sort using java 8
从
Java 8
开始,我们不必直接使用Collections.sort()
。List
接口有一个默认的sort()
方法:参见 http://visvv.blogspot.in/2016/01/sorting-objects-in-java-8.html。
From
Java 8
and onward we don't have to useCollections.sort()
directly.List
interface has a defaultsort()
method:See http://visvv.blogspot.in/2016/01/sorting-objects-in-java-8.html.
Java 8 Lambda 缩短了排序。
Java 8 Lambda shortens the sort.
您可以使用 Bean 比较器 对自定义中的任何属性进行排序班级。
You can use the Bean Comparator to sort on any property in your custom class.
是的,例如在 这个答案< /a> 我按类
IndexValue
的属性v
排序如果您注意到这里我正在创建一个匿名内部类(这是Java 闭包)并将其直接传递给类
Arrays
的sort
方法。您的对象也可以实现
Comparable
(这就是 String 和大多数Java 中的核心库确实如此),但这将定义类本身的“自然排序顺序”,并且不允许您插入新的类。Yes, that's possible for instance in this answer I sort by the property
v
of the classIndexValue
If you notice here I'm creating a anonymous inner class ( which is the Java for closures ) and passing it directly to the
sort
method of the classArrays
Your object may also implement
Comparable
( that's what String and most of the core libraries in Java does ) but that would define the "natural sort order" of the class it self, and doesn't let you plug new ones.我发现大多数(如果不是全部)这些答案都依赖于底层类(对象)来实现可比较的或具有辅助可比较的接口。
不是我的解决方案!以下代码可让您通过了解对象的字符串名称来比较对象的字段。您可以轻松地修改它以不使用该名称,但随后您需要公开它或构造您想要比较的对象之一。
I found most if not all of these answers rely on the underlying class (Object) to implement comparable or to have a helper comparable interface.
Not with my solution! The following code lets you compare object's field by knowing their string name. You could easily modify it not to use the name, but then you need to expose it or construct one of the Objects you want to compare against.
您可以尝试使用 Guava 订购 :
You can try Guava Ordering:
如果您使用 Java 8 或更旧版本,这是最好的解决方案。
在这种情况下,它将首先使用“getCgpa”进行排序,第二部分将使用 getFname 和 getId 进行排序。这是 pojo 类中的字段。
Well if you using Java 8 or older version Here is the Best solution.
In this case, it will first sort with 'getCgpa' first and for the second part it will sort with getFname and getId. Which is field into the pojo class.
您的 customComparator 类必须实现 java.util.Comparator 才能使用。
它还必须覆盖compare()并且equals()
compare()必须回答这个问题:对象1小于、等于还是大于对象2?
完整文档: http://java.sun .com/j2se/1.5.0/docs/api/java/util/Comparator.html
your customComparator class must implement java.util.Comparator in order to be used.
it must also overide compare() AND equals()
compare() must answer the question: Is object 1 less than, equal to or greater than object 2?
full docs: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Comparator.html
此代码片段可能有用。如果你想对一个对象进行排序
就我而言,我想按 VolumeName 排序:
这有效。我在我的jsp中使用它。
This code snippets might be useful. If you want to sort an Object
in my case I want to sort by VolumeName:
This works. I use it in my jsp.
使用此库此处,您可以对列表进行排序多列上的自定义对象。该库使用 8.0 版本的功能。那里也提供样品。这是一个要做的示例
With this library here you can sort the list of custom objects on multiple columns. The library uses version 8.0 features. Sample is also available there. Here is a sample to do
您可以查看在 Java 论坛上举行的演示文稿 2016年德国斯图加特。
只有少数幻灯片使用德语,99%的内容是“基于英语”的Java源代码;就像
OurCustomComparator
使用默认方法(以及其他有趣的想法)一样。如图所示,导致非常简洁的代码选择一些 getter 方法进行排序;以及超级简单的排序标准链接(或反转)。如果您喜欢 java8,您会发现有很多材料可以帮助您入门。
You can have a look into this presentation hold at the Java Forum in Stuttgart Germany in 2016.
Only a few slides use German language, 99% of the content is "English based" Java source code; like
where
OurCustomComparator
is using default methods (and other interesting ideas). As shown, leading to very concise code to pick some getter method for sorting; and super simple chaining (or reversing) of sort criteria.If you are into java8, you find a lot of material there to get you started.
自 1.8 以来新增的是 List.sort() 方法,而不是使用 Collection.sort()
所以你直接调用 mylistcontainer.sort()
下面是演示 List.sort() 功能的代码片段:
Fruit 类是:
New since 1.8 is a List.sort() method instead of using the Collection.sort()
so you directly call mylistcontainer.sort()
Here is a code snippet which demonstrates the List.sort() feature:
The Fruit class is:
使用 java-8 流 api,您可以通过以下方式对 ArrayList 进行排序:
using the java-8 stream api you can sort an
ArrayList
by:我更喜欢这个过程:
如果你的对象列表有一个名为
startDate
的属性,你可以一遍又一遍地调用它。您甚至可以将它们链接起来startDate.time
。这要求您的对象是
Comparable
,这意味着您需要一个compareTo
、equals
和hashCode
实现。是的,它可能会更快......但是现在您不必为每种类型的排序创建一个新的比较器。如果您可以节省开发时间并放弃运行时,您可能会选择这个。
I prefer this process:
If you list of objects has a property called
startDate
, you call use this over and over. You can even chain themstartDate.time
.This requires your object to be
Comparable
which means you need acompareTo
,equals
, andhashCode
implementation.Yes, it could be faster... But now you don't have to make a new Comparator for each type of sort. If you can save on dev time and give up on runtime, you might go with this one.
使用 Java 8 可以使用
Comparator.comparing()
在一行中定义Comparator
使用以下任一方式:
选项 1:
选项2:
Using Java 8 use can define the
Comparator
in one line usingComparator.comparing()
Use any of the following way:
Option 1:
Option 2:
您的自定义类可以实现“Comparable”接口,这需要实现 CompareTo 方法。在 CompareTo 方法中,您可以定义一个对象小于或大于另一个对象的含义。因此,在您的示例中,它可能看起来像这样:
...........
负数表示 this 小于所比较的对象。正数表示 this 大于所比较的对象,零表示对象相等。
然后,您可以使用 collections.sort(myList) 对列表进行排序,而无需输入比较器。如果您使用排序的集合数据结构(例如 TreeSet 或 TreeMap),此方法还具有自动排序的优点。
如果您想了解有关 Comparable 接口的更多信息,可以查看这篇文章(披露:我是作者;))
https://nullbeans.com/the-java-comparable -接口自动排序集合/
Your custom class can implement the "Comparable" interface, which requires an implementation of the CompareTo method. In the CompareTo method, you can then define what it means that an object is less than or more than the other object. So in your example, it can look something like this:
..........
A negative number indicates that this is smaller than the object being compared to. A positive number indicates that this is larger than the compared to object and a Zero means that the objects are equal.
You can then use the collections.sort(myList) to sort your list without having to feed in a comparator. This method also has the advantage of having things sorted automatically if you use a sorted collection data structures like a TreeSet or a TreeMap.
You can check this article if you would like to read more about the Comparable interface (disclosure: I am the author ;) )
https://nullbeans.com/the-java-comparable-interface-automatic-sort-of-collections/
您还可以使用 Springs PropertyComparator 如果您只有一个指向要排序的(嵌套)属性的 String 属性路径:
缺点是,此比较器会默默地忽略不存在或不可访问的属性,并将其处理为 用于比较的 null 值。这意味着,您应该仔细测试这样的比较器或以某种方式验证属性路径的存在。
You could also use Springs PropertyComparator if you have just a String property path to the (nested) property you want to sort:
The drawback is, that this comparator silently ignores properties which does not exist or are not accessible and handles this as null value for comparison. This means, you should carefully test such a comparator or validate the existence of the property path somehow.
我已经尝试了互联网上提供的许多不同的解决方案,但适合我的解决方案可以在下面的链接中找到。
https://www.java67。 com/2017/07/how-to-sort-arraylist-of-objects-using.html
I have tried lots of different solutions available on internet but solution which works for me is available at below link.
https://www.java67.com/2017/07/how-to-sort-arraylist-of-objects-using.html