Sanoop Surendran
Sanoop Surendran

Reputation: 3486

Sort two dependent string arrays

I Have two String arrays in my application, One containing Country names, and other containing corresponding extension code, But the issue is that the names of countries are not properly ordered in alphabetical order,

     public static final String[] m_Countries = {
        "---select---", "Andorra", ....,  "Zimbabwe"};
     public static final String[] m_Codes = {
        "0", "376",...., "263"};

These are the arrays,

So my question is, is there any way to sort the first array such that the second array also changes to corresponding position without writing my own code? If not, what's the best sort method i can use for these arrays? Any kind of help will be greatly appreciated.

Upvotes: 2

Views: 241

Answers (2)

Dhaval Patel
Dhaval Patel

Reputation: 10299

Form TreeMap from your array and all your data get sort. After that fill your respective array with Key and Values.

TreeMap<String, String> map = new TreeMap<>();
int length = m_Countries.length;
for(int i=0;i<length;i++){
    map.put(m_Countries[i], m_Codes[i]);
}
String[] countries = map.keySet().toArray(new String[map.keySet().size()]);
System.out.println("Country:"+Arrays.toString(countries));
String[] codes =   map.values().toArray(new String[map.values().size()]);
System.out.println("Codes:"+Arrays.toString(codes));

Result:

Country:[---select---, Afghanistan, ..., Zimbabwe]
Codes:[0, 93,.... , 263]

Upvotes: 2

hyongbai
hyongbai

Reputation: 254

Method 1.

You can create a hashMap to store the original country to code.

private void handle(String[] m_Countries, String[] m_Codes, Map<String, String> map) {
    if (m_Codes == null || m_Countries == null || map == null) {
        return;
    }
    //
    final int codeCount = m_Codes.length;
    final int countryCount = m_Countries.length;
    final int count = Math.min(codeCount, countryCount);
    for (int i = 0; i < count; i++) {
        map.put(m_Countries[i], m_Codes[i]);
    }
    // TODO sort
    // get code by country name by map.get(country)
}

Method 2.

You can make a List of pairs which contains country and code. Then sort the list.

private List<Pair<String, String>> sortCountryWithCode(String[] m_Countries, String[] m_Codes) {
    if (m_Codes == null || m_Countries == null) {
        return null;
    }
    //
    final int codeCount = m_Codes.length;
    final int countryCount = m_Countries.length;
    final int count = Math.min(codeCount, countryCount);
    if (count == 0) {
        return null;
    }
    // generate a list
    List<Pair<String, String>> list = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        list.add(new Pair<String, String>(m_Countries[i], m_Codes[i]));
    }
    // sort
    Collections.sort(list, new Comparator<Pair<String, String>>() {
        @Override
        public int compare(Pair<String, String> lhs, Pair<String, String> rhs) {
            return lhs.first.compareToIgnoreCase(rhs.first);
        }
    });
    return list;
}

code with love. :)

Upvotes: 2

Related Questions