-2

Currently i am getting data from my list but i get the data randomly, if anyone can help me to sort data on the basis of dayInNumber i have attached the picture of my database. I want to sort the data like 1 2 3 4 .. 14 enter image description here

Thanks

Saad Zahoor
  • 138
  • 1
  • 9
  • Does this answer your question? [Sort ArrayList of custom Objects by property](https://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) –  Feb 04 '21 at 19:56

2 Answers2

0

for sorting lists we can use sort(your_list) method of Collections class. This will sort the list as per the natural ordering i.e. ascending order. Since you want it sorted on the basis of a particular feature you should implement Comparator interface with type safety same as of your list. You will have to implement compareTo method. Refer to the below code for FYR:- `

public class Temp implements Comparable<Temp> {
    Temp(int a, int b) {
        this.a = a;
        this.b = b;
    }

    int a;
    int b;
    public static void main(String[] args) {

        Temp t1 = new Temp(11,2);
        Temp t2 = new Temp(9,5);
        Temp t3 = new Temp(1,7);
        Temp t4 = new Temp(10,9);
        Temp t5 = new Temp(14,11);

        ArrayList<Temp> al = new ArrayList<Temp>();
        al.add(t1);
        al.add(t2);
        al.add(t3);
        al.add(t4);
        al.add(t5);
        Collections.sort(al);
        for(Temp i:al) {
            System.out.println(i.a+"," +i.b);
        }
    }

    @Override
    public int compareTo(Temp o) {
        if(a==o.a) {
            return 0;
        }
        else if(a>o.a) {
            return 1;  
        }
        else {
            return -1;
        }
    }
}`
Sharad Nanda
  • 406
  • 1
  • 15
  • 27
0

Suppose you have Client class defined as

class Client {

    private long dayInNumber;
    private String clientName;
    private String day;

   //setters and getters ommitted for brevity and clarity
}

Then you have a list of clients

List<Client> clients = new ArrayList<>();

To sort that dayInNumber you can either sort that in SQL (order by clause) assuming the data is coming from SQLite.

Or you can create a custom Comparator<T> of Client objects to do the sorting in code as below

import java.util.Comparator;

public class ClientDayInNumberComparator implements Comparator<Client> {

    @Override
    public int compare(Client c1, Client c2) {
        if(c1.getDayInNumber() > c2.getDayInNumber()) return 1;
        else if(c1.getDayInNumber() < c2.getDayInNumber()) return -1;
        else
            return 0;
    }

}

This will sort clients by dayInNumber in ascending order (1, 2, 3,...N). Then use it as follows

List<Client> clients = new ArrayList<>();
Collections.sort(clients, new ClientDayInNumberComparator());

Collections is packed in java.utils package which you'll need to import

Leo
  • 14,625
  • 2
  • 37
  • 55
  • Thank you for you time. after calling clients = new ArrayList<>(); the contents or items present in my list become 0 – Saad Zahoor Apr 29 '19 at 03:48
  • @saadzahoor Obviously, You need to fill the list with proper data, your data! Which is out of the scope of this question...so, you can safely ignore that line – Leo Apr 29 '19 at 04:03