2

I have a java list that i need to sort after adding objects to it. the object has three elements. a string and two integers. the class is as follows:-

public class ValueWords implements Comparable<ValueWords>{

//.............................................

public static final Comparator<ValueWords> valComparator = new Comparator<ValueWords>()
 {@Override
    public int compare(ValueWords v1, ValueWords v2) {
        return v1.valWord   - v2.valWord; }
   }
};
//....................................................................
private String strWord;
private int valWord;
private int phraseWord;
public int getPhraseWord() {
    return phraseWord;}
public void setPhraseWord(int phraseWord) {
    this.phraseWord = phraseWord;}
public String getStrWord() {
    return strWord;}
public void setStrWord(String strWord) {
    this.strWord = strWord;}
public int getValWord() {
    return valWord;}
public void setValWord(int valWord) {
    this.valWord = valWord;}
public ValueWords() {}

@Override
public int compareTo(ValueWords arg0) {
    // TODO Auto-generated method stub
    return 0;}
//..............................................
}

i call it like this

 Collections.sort(valWordList,ValueWords.valComparator);

it gives me the sorted list as follows:-

 ---------------------------------------------
   strWord     valWord      phraseWord
   abcd          0             1
   abcde         0             0
   mns           1             2 
   efgh          1             1
   xyz           2             2
   zxx           2             0
  --------------------------------------------

i want a nested sorting like:-

  ---------------------------------------------
   strWord     valWord      phraseWord
   abcde        0             0
   abcd         0             0
   efgh         1             1 
   mns          1             2
   zxx          2             0
   xyz          2             2
  --------------------------------------------

i have followed made few futile tries but being new to java. they did not work. and they are quiet primitive to be placed here.

3
  • 1
    It's not clear how you want things to be sorted, apparently by valWord, then phraseWord, then strWord? Seems straightforward. Commented Nov 26, 2014 at 14:45
  • sir by valWord then phraseWord. Commented Nov 26, 2014 at 15:11
  • 1
    Do the first comparison. If they're not equal, you're done, return the results of that comparison. If they're equal, return the results of the second comparison. Commented Nov 26, 2014 at 15:12

2 Answers 2

1

try changing your comparator to check for both values:

public static final Comparator<ValueWords> valComparator = new Comparator<ValueWords>() {
    @Override
    public int compare(ValueWords v1, ValueWords v2) {
        if(v1.valWord == v2.valWord)
            return v1.phraseWord - v2.phraseWord;
        else 
            return v1.valWord - v2.valWord;
    }
};
Sign up to request clarification or add additional context in comments.

1 Comment

Using Integer.compare instead of just substracting the ints would also allow pairs of ints where the difference overflows.
1

You can combine both in a single comparator:

public static final Comparator<ValueWords> valComparator = new Comparator<ValueWords>()
{
    @Override
    public int compare(ValueWords v1, ValueWords v2) {
        int result = Integer.compare(v1.valWord, v2.valWord);
        return result == 0 ? Integer.compare(v1.phraseWord, v2.phraseWord) : result;
    }
};

In java 8+ you can also use Comparator.thenComparing to combine 2 comparators:

valComparator = ((Comparator<ValueWords>) (v1, v2) -> Integer.compare(v1.valWord, v2.valWord))
                                         .thenComparing((v1, v2) -> Integer.compare(v1.phraseWord, v2.phraseWord));

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.