Quantcast
Channel: How to sort List of objects by some property - Stack Overflow
Browsing all 19 articles
Browse latest View live

Answer by Dimitar Milanov for How to sort List of objects by some property

Here's what did the trick for me.Was much shorter and easier than everything else I found:Collections.sort(listName, Comparator.comparing(Object::getProperty).reversed());The ".reversed()" part at the...

View Article



Answer by Gergana Toncheva for How to sort List of objects by some property

public class ActiveAlarm { public long timeStarted; public long timeEnded; private String name = ""; private String description = ""; private String event; private boolean live =...

View Article

Answer by djklicks-dhananjay for How to sort List of objects by some property

The best and the easiest way to sort any list of objects in Java (Java 8 and above).Lets sort a basket of fruits based on the property "fruitName"Fruit POJO:class Fruit{ int price; String fruitName;...

View Article

Answer by nayan for How to sort List of objects by some property

Employee POJO Classpackage in.ac.adit.oop.sort;public class Employee { private int id; private String name; private String department; public int getId() { return id; } public Employee() { super(); }...

View Article

Answer by Frank Kevy for How to sort List of objects by some property

We can use the Comparator.comparing() method to sort a list based on an object's property.class SortTest{ public static void main(String[] args) { ArrayList<ActiveAlarm> activeAlarms = new...

View Article


Answer by Arvind Kumar Avinash for How to sort List of objects by some property

Java-8 solution using Stream API:A. When timeStarted and timeEnded are public (as mentioned in the requirement) and therefore do not (need to) have public getter methods:List<ActiveAlarm> sorted...

View Article

Answer by Karthikeyan for How to sort List of objects by some property

In java8+ this can be written in single line as follows:collectionObjec.sort(comparator_lamda) or...

View Article

Answer by Mathias G. for How to sort List of objects by some property

Since Java8 this can be done even cleaner using a combination of Comparator and Lambda expressionsFor Example:class Student{ private String name; private List<Score> scores; // +accessor...

View Article


Answer by John Fowler for How to sort List of objects by some property

JAVA 8 and Above Answer (Using Lambda Expressions)In Java 8, Lambda expressions were introduced to make this even easier! Instead of creating a Comparator() object with all of it's scaffolding, you can...

View Article


Answer by Alireza Fattahi for How to sort List of objects by some property

As mentioned you can sort by:Making your object implement ComparableOr pass a Comparator to Collections.sortIf you do both, the Comparable will be ignored and Comparator will be used. This helps that...

View Article

Answer by Jagadeesh for How to sort List of objects by some property

We can sort the list in one of two ways:1. Using Comparator : When required to use the sort logic in multiple placesIf you want to use the sorting logic in a single place, then you can write an...

View Article

Answer by Kal for How to sort List of objects by some property

public class ActiveAlarm implements Comparable<ActiveAlarm> { public long timeStarted; public long timeEnded; private String name = ""; private String description = ""; private String event;...

View Article

Answer by Richard H for How to sort List of objects by some property

In java you need to use the static Collections.sort method. Here is an example for a list of CompanyRole objects, sorted first by begin and then by end. You can easily adapt for your own object....

View Article


Answer by 卢声远 Shengyuan Lu for How to sort List of objects by some property

Guava's ComparisonChain:Collections.sort(list, new Comparator<ActiveAlarm>(){ @Override public int compare(ActiveAlarm a1, ActiveAlarm a2) { return ComparisonChain.start()...

View Article

Answer by MarcoS for How to sort List of objects by some property

You can use Collections.sort and pass your own Comparator<ActiveAlarm>

View Article


Answer by Jon Skeet for How to sort List of objects by some property

Either make ActiveAlarm implement Comparable<ActiveAlarm> or implement Comparator<ActiveAlarm> in a separate class. Then call:Collections.sort(list);orCollections.sort(list, comparator);In...

View Article

Answer by Liv for How to sort List of objects by some property

You can call Collections.sort() and pass in a Comparator which you need to write to compare different properties of the object.

View Article


Answer by jmj for How to sort List of objects by some property

Using ComparatorFor Example:class Score { private String name; private List<Integer> scores; // +accessor methods} Collections.sort(scores, new Comparator<Score>() { public int...

View Article

How to sort List of objects by some property

I have simple class public class ActiveAlarm { public long timeStarted; public long timeEnded; private String name = ""; private String description = ""; private String event; private boolean live =...

View Article
Browsing all 19 articles
Browse latest View live




Latest Images