0

I have java class called Country.

public class Conutry {
private String Code;
private String name;

public Conutry(String code, String name) {
    Code = code;
    this.name = name;
}

public String getCode() {
    return Code;
}

public void setCode(String code) {
    Code = code;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

And I need to create a list of countries ordered by name. However, United States and Canada should be the first two items in the list and the rest are sorted alphabetically by country name.knowing that I don't have access to Country class.I know that I have to use Comparator but I don't know the exact logic here.

public class TestDrive {
public static void main(String[] args) {
    List<Conutry> list = new ArrayList<Conutry>();
    list.add(new Conutry("AR","Argentina"));
    list.add(new Conutry("AL","Albania"));
    list.add(new Conutry("US","United States of America"));
    list.add(new Conutry("JO","Jordan"));
    list.add(new Conutry("DZ","Algeria"));
    list.add(new Conutry("CA","Canada"));
    list.add(new Conutry("FR","France"));
    list.add(new Conutry("TR","Turkey"));
    list.add(new Conutry("BR","Brazil"));
    list.add(new Conutry("AE","United Arab Emirates"));

    //This is my code. I think there should be a better solution
    Comparator<Conutry> comparator = new Comparator<Conutry>() {
        @Override
        public int compare(Conutry c1, Conutry c2) {
            if("US".equals(c1.getCode()))
                return -1;
            if("CA".equals(c1.getCode()))
                if(c2.getCode()!="US")
                    return -1;
                else
                    return 1;

            if("US".equals(c2.getCode()))
                return 1;

            if("CA".equals(c2.getCode()))
                if("US".equals(c1.getCode()))
                    return -1;
                else
                    return 1;

             return c1.getName().compareTo(c2.getName()) ;
        }
    };

    list.stream().sorted(comparator).forEach(c-> System.out.println(c.getName()));


}

}

Mido
  • 17
  • 2
  • 3
    You will want to do some research into "custom comparators" – MadProgrammer Nov 21 '19 at 00:36
  • 2
    Or add all but US and Canada, sort, then `insert` US and Canada ? – Scary Wombat Nov 21 '19 at 00:38
  • 1
    And afterwards use [sort](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#sort-java.util.Comparator-)! – csabinho Nov 21 '19 at 00:39
  • 2
    Here's a more compact comparator: `Map priority = Map.of("US", -2, "CA", -1);` `Comparator comparator = Comparator.comparingInt(country -> priority.getOrDefault(country.getCode(), 0)).thenComparing(Country::getName, Comparator.naturalOrder());` – boot-and-bonnet Nov 21 '19 at 02:13
  • 1
    @boot-and-bonnet Thank you so much, this worked fine for me. – Mido Nov 21 '19 at 02:50
  • 1
    No problem - I wasn't able to edit the comment, but you don't actually need the `, Comparator.naturalOrder()` at the end – boot-and-bonnet Nov 21 '19 at 02:53

1 Answers1

0

It goes something like this:

public static void main(String[] args) {
    List<Conutry> list = new ArrayList<Conutry>();

    // putting these two at the start of the list
    list.add(new Conutry("US","United States of America"));
    list.add(new Conutry("CA","Canada"));

    // building the rest of the list
    list.add(new Conutry("AR","Argentina"));
    list.add(new Conutry("AL","Albania"));
    list.add(new Conutry("JO","Jordan"));
    list.add(new Conutry("DZ","Algeria"));
    list.add(new Conutry("FR","France"));
    list.add(new Conutry("TR","Turkey"));
    list.add(new Conutry("BR","Brazil"));
    list.add(new Conutry("AE","United Arab Emirates"));

    // comparator that compares conutry objects based on their names
    public class ConutryComparator implements Comparator<Conutry> {
        public int compare(Conutry a, Conutry b) {
            // I will leave the null handling part as exercise
            return a.getName().compareTo(b.getName());
        }
    }

    // then sort the sublist starting with third element (index 2)
    Collections.sort(list.sublist(2, list.size(), new ConutryComparator());

}

Done

Edward Aung
  • 3,014
  • 1
  • 12
  • 15