4

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.

3
  • Which examples you tried? And what is not clear Commented Jan 6, 2014 at 12:33
  • There is a method called compare(Object O1, Object O2) in the Comparator interface. You have to override(implement) it. Put your sorting logic in there... Logic is simple... if O1.ms<O2.ms ? return -1 :((O1.ms==O2.ms)?return 0 :return 1)); .. Something like this will help ...NOTE: This is not the exact code... Commented Jan 6, 2014 at 12:33
  • @TheLostMind +1 worked perfectly.. Commented Jan 6, 2014 at 13:10

6 Answers 6

16

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);
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to call youradapter.notifyDataSetChanged(); so that the changes take place immediately.
3

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;
        }
    }
}

Comments

2

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());

Comments

1

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.

Comments

0
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)
}

Comments

0

@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 .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.