简体   繁体   中英

How implement comparator in to sort objects in android or java

Let me start with the following example:

public void loadList(ArrayList<Calls> list) {
    List<Calls> calls = new ArrayList<Calls>();
    calls.addAll(list);
}

where Calls is a simple class:

public class Calls {
    public long ms;
    public name;     
} 

I want to sort the List<Calls> above in ascending or descending order based on the ms field. I came across some examples of Comparator but still am not clear through.

I guess this would help

public void loadList(ArrayList<Calls> list) {
    List<Calls> calls = new ArrayList<Calls>();
    calls.addAll( list );

    // Ascending Order
    Collections.sort(calls, new Comparator<Calls>() {

        @Override
        public int compare(Calls o1, Calls o2) {
            return (int)(o1.ms-o2.ms);
        }
    });
    // Descending Order
    Collections.sort(calls, new Comparator<Calls>() {

        @Override
        public int compare(Calls o1, Calls o2) {
            return (int)(o2.ms-o1.ms);
        }
    });
}

just implement a Comparator this way:

private class CallsComparator implements Comparator<Calls> {

    @Override
    public int compare(Calls calls1, Calls calls2) {
      //Swap calls1 with 2
      return 1;
      //Spap 2 with 1
      return -1;
      // do nothing
      return 0;
    }

  }

of course you have to replace my comments with an if-condition :-)

and execute the comparator with:

  Collections.sort(calls, new CallsComparator());

see following example

class CallsComp implements Comparator<Calls>{

    @Override
    public int compare(Calls c1, Calls c2) {
        if(c1.getMs() < c2.getMs()){
            return 1;
        } else {
            return -1;
        }
    }
}
class MSComaparator implements Comparator<Calls>{

        @Override
        public int compare(Calls lhs, Calls rhs) {
            // TODO Auto-generated method stub
            return lhs.ms-rhs.ms;
        }

    }

    class NameComaparator implements Comparator<Calls>{

        @Override
        public int compare(Calls lhs, Calls rhs) {
            // TODO Auto-generated method stub
            return lhs.name.comapreTo(rhs.name);
        }

    }

and call:

public void loadList(ArrayList<Calls> list) {
    List<Calls> calls = new ArrayList<Calls>();
    calls.addAll(list);
    Collections.sort(calls,new MSComaparator())
}

if you just want to sort based on ns you can simple implement Comparable:

public class Calls implements Comparable<Calls>{
    public long ms;
    public name;     
    @Override
        public int compareTo(Calls  another) {
            // TODO Auto-generated method stub
            return this.ms>another.ms;
        }
} 

And call:

public void loadList(ArrayList<Calls> list) {
    List<Calls> calls = new ArrayList<Calls>();
    calls.addAll(list);
    Collections.sort(calls)
}

The Calls class need to implement the Comparable interface and implement the compareTo method:

public class Calls implements Comparable<Calls> {
   public long ms;
   public name;

   @Override
   public int compareTo(Calls call) {
       // Prepend a -1 for inverse order
       return Long.compare(this.ms,call.ms);
   }
} 

Then just call Collections.sort(calls) to sort your list.

@ARP

I know its too late to answer this question but I have implemented same thing in my project recently so thought i should share with you

so inside your function after adding list what you can do is

public void loadList(ArrayList<Calls> list) {
    List<Calls> calls = new ArrayList<Calls>();
    calls.addAll(list);
    Collections.sort(calls, new Comparator<Calls>() {
                @Override
                public int compare(Calls o, Calls t1) {
                    return (int) (o.getMS() - t1.getMS());
                }
            });
}

This will sort ArrayList<Calls> call for you based on the value present in ms .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM