omarsafwany
omarsafwany

Reputation: 3811

sort descendingly based on multiple parameters in Java

I have input of multiple strings where I parse each one to fetch certain data and then display them accordingly. The point is I need to sort them accodring to some parameters before displaying them. To clarify:

String s1 = "a1 5 2014-12-05";
String s2 = "a2 10 2014-12-06";
String s3 = "a3 5 2014-12-04":

After pasring each string, I need to sort them descendingly first according to the second parameter and then according to the date whenever I reach a tie for example.

Output should be:

a2, a1, a3

Any idea how can this be achieved or if I could change the way to a more efficient one?!

Upvotes: 2

Views: 248

Answers (1)

sol4me
sol4me

Reputation: 15698

You can parse the data and use Collections#sort. Your class can define some natural ordering using Comparable and in case if you want to sort descending then just pass Comparator.reverseOrder() as second arg to Collections#sort like this

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class DateSorter {

    static class Entry implements Comparable<Entry> {
        String id;
        int num;
        Date date;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        public Entry(String id, int num, String date) throws ParseException {
            this.id = id;
            this.num = num;
            this.date = sdf.parse(date);
        }

        @Override
        public int compareTo(Entry o) {
            return date.compareTo(o.date);
        }

        @Override
        public String toString() {
            return id + '\'';
        }
    }

    public static void main(String[] args) throws ParseException {
        String s1 = "a1 5 2014-12-05";
        String s2 = "a2 10 2014-12-06";
        String s3 = "a3 5 2014-12-04";

        String[] split = s1.split(" ");

        List<Entry> entries = new ArrayList<>();
        entries.add(new Entry(split[0], Integer.parseInt(split[1]), split[2]));
        split = s2.split(" ");
        entries.add(new Entry(split[0], Integer.parseInt(split[1]), split[2]));
        split = s3.split(" ");
        entries.add(new Entry(split[0], Integer.parseInt(split[1]), split[2]));

    Collections.sort(entries, new Comparator<Entry>() {
        @Override
        public int compare(Entry o1, Entry o2) {
            return o2.compareTo(o1);
        }
    });

        System.out.println(entries);
    }
}

Output

[a2', a1', a3']

Upvotes: 2

Related Questions