0

Present i am using Comparator to sort my objects .I have the items in list as follows

   FIRST ITEM
   SECOND ITEM
   THIRD ITEM
   LAST ITEM
   FORTH ITEM

Comparator code is:

public static Comparator<PartyResultVO> sortData = new Comparator<PartyResultVO>()
{

     public int compare(VO vo1, VO vo2)
     return (vo1.getName()).compareTo(vo2.getName());
};

It is working perfectly to sort.But what i want is i need to put the item with name LAST ITEM at last.How i can exclude only that object and place at the end.Is there any way to do that.

Please help me.Thanks in advance...

4 Answers 4

5

You can just add special logic for that case:

public static Comparator<VO> sortData = new Comparator<VO>()
{
     public int compare(VO vo1, VO vo2) {
         if (vo1.getName().equals("LAST ITEM")) {
              if (vo2.getName().equals("LAST ITEM")) {
                  return 0;
              } else {
                  return 1;
              }
         else if (vo2.getName().equals("LAST ITEM")) {
              return -1;
         }
         return (vo1.getName()).compareTo(vo2.getName());
     }
};
Sign up to request clarification or add additional context in comments.

9 Comments

Make sure comparing two "LAST ITEM"s return 0 though.
@KennyTM: That doesn't matter when it comes to sorting, but it might be more complete to do it.
But i am not sure about the name exaclty.But it contains the word last
On an unrelated note: I was in Keppil Way last week.
@PSR You appears to be physically able to type contains so I assume so.
|
2

Can you try this ?

Collections.sort(list, new Comparator<PartyResultVO>() {

@Override
public int compare(VO vo1, VO vo2) {
    if (vo1.getName().equals("LAST ITEM")) {
          if (vo2.getName().equals("LAST ITEM")) {
              return 0;
          } else {
              return 1;
          }
     else if (vo2.getName().equals("LAST ITEM")) {
          return -1;
     }
        return  vo1.getName().compareTo(vo2.getName());
    }


});

Comments

2

You could introduce state to your comparator and make it work exceptionally with certain values, return an integer that guarantees it will be the last one. Extended example of Keppil's comparator:

class PartyResultComparator implements Comparator<PartuResultVO> {
    String exceptionalValue;

    public PartyResultComparator(String exceptionalValue) {
        this.exceptionalValue = exceptionalValue;
    }

    public int compare(VO vo1, VO vo2) {
         if (isExceptional(vo1.getName())) {
              return 1;
         else if (isExceptional(vo2.getName())) {
              return -1;
         }

         return (vo1.getName()).compareTo(vo2.getName());
    }

    private boolean isExceptional(String value) {
        // is this value exceptional?
    }
}

Comments

0

A shorter implementation

public static Comparator<PartyResultVO> sortData = new Comparator<PartyResultVO>() {
     public int compare(VO vo1, VO vo2) {
         return mapToLast(vol.getName()).compareTo(mapToLast(vo2.getName()));
     }
    String mapToLast(String s) {
        return s.contains("LAST") ? "\uFFFF" : s;
    }
}

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.