1

Hi I have a country class and that contains a countryLanguage set. I want to sort the language for each country. I am createing view page. country and his corresponding language. county is ok but its language are not in sorted order. I am confused in which class i have use compartor and how.

public class Country implements Serializable{
private Set<CountryLanguage> countryLanguage = new HashSet<CountryLanguage>(0);
//...
}
public class CountryLanguage {
private CountryLanguageID countryLangPK = new CountryLanguageID();
//...
}

composit id class

   public class CountryLanguageID implements Serializable,Comparable<CountryLanguageID>{

private Country country;
private Language language;

@ManyToOne(fetch=FetchType.LAZY)
public Country getCountry() {
    return country;
}
public void setCountry(Country country) {
    this.country = country;
}

@ManyToOne(fetch=FetchType.LAZY)
public Language getLanguage() {
    return language;
}
public void setLanguage(Language language) {
    this.language = language;
}

public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    CountryLanguageID that = (CountryLanguageID) o;

    if (country != null ? !country.equals(that.country) : that.country != null){
        return false;
    }
    if (language != null ? !language.equals(that.language) : that.language != null){
        return false;
    }
    return true;
}
public int hashCode() {
    int result;
    result = (country != null ? country.hashCode() : 0);
    result = 31 * result + (language != null ? language.hashCode() : 0);
    return result;
}
@Override
public int compareTo(CountryLanguageID o) {

    //return this.language.compareTo(o.language);
    return this.getLanguage().getLanguageName().compareTo(o.getLanguage().getLanguageName());
}
}

1 Answer 1

2

You can use a TreeSet instead of HashSet to keep countryLanguage:

private Set<CountryLanguage> countryLanguage = new TreeSet<CountryLanguage>(0);

The elements of TreeSet are ordered using their natural ordering or can be ordered by using a Comparator, typically provided at TreeSet creation time.

If you want to go with natural ordering of CountryLanguage, make CountryLanguage implement Comparable:

public class CountryLanguage implements Comparable<CountryLanguage>{

@Override
public int compareTo(CountryLanguage cl) {
    // Comparison logic
}
...
}

And if you want to use a Comparator to order elements of countryLanguage, define a comparator:

private static final Comparator<CountryLanguage> COMP = new Comparator<CountryLanguage>() {

        @Override
        public int compare(CountryLanguage o1, CountryLanguage o2) {
            // Compare o1 with o2
        }
};

and use it while creating your TreeSet:

 private Set<CountryLanguage> countryLanguage = new TreeSet<CountryLanguage>(COMP);

EDIT:

Your set is of type CountryLanguage. So in order to sort the elements of Set<CountryLanguage> countryLanguage, you need to make CountryLanguage implement Comparable (but you have defined CountryLanguageID to implement Comparable):

And while comparing instances of CountryLanguage, you can use CountryLanguageID property for comparison:

public class CountryLanguage implements Comparable<CountryLanguage>{
private CountryLanguageID countryLangPK = new CountryLanguageID();
...    
@Override
public int compareTo(CountryLanguage cl) {
return this.countryLangPK.getLanguage().getLanguageName().compareTo(cl.getCountryLangPK().getLanguage().getLanguageName());
}
...
}
Sign up to request clarification or add additional context in comments.

3 Comments

its not working. I have tried both of logics. In the first logic I written comparision login in countryLanguage class as this.getCountryLangPK().getLanguage().getLanguageName().compareTo(o2.getCountryLangPK().getLanguage().getLanguageName()); any more suggessions is appriceated
I have tried overriding compareTo in country,countryLanguage,countryLanguageId and language class but fail to get country object with sorted language, should I use @Sort.
you have Country with a set of CountryLanguage and your CountryLanguage is implementing Comparable as it was shown in my answer (to compare CountryLanguage you can use any prop of it, mine was just an example).Now when you create a new Country and you start putting CountryLanguage into the set, the TreeSet itself will maintain the ordering of the elements.there is no need to sort the elements using a different sort method.And again, your set is of type CountryLanguage, so CountryLanguage should implement Comparable.

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.