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

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

$
0
0
public class ActiveAlarm implements Comparable<ActiveAlarm> {    public long timeStarted;    public long timeEnded;    private String name = "";    private String description = "";    private String event;    private boolean live = false;    public int compareTo(ActiveAlarm a) {        if ( this.timeStarted > a.timeStarted )            return 1;        else if ( this.timeStarted < a.timeStarted )            return -1;        else {             if ( this.timeEnded > a.timeEnded )                 return 1;             else                 return -1;        } }

That should give you a rough idea. Once that's done, you can call Collections.sort() on the list.


Viewing all articles
Browse latest Browse all 19

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>