Displaying Countries in sorted order
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   prasannaladdha_laddha
Posted On:   Wednesday, October 24, 2001 09:04 AM

I am storing my country key and value pair in an TreeMap, I want to display countries in sorted order of countries names.I dont want to do this operation on database side.


And I want to use Collection like TreeMap or any others..

I am populating list in html with this.
Can any one crack this problem.

Re: Displaying Countries in sorted order

Posted By:   Anonymous  
Posted On:   Tuesday, November 6, 2001 09:54 AM

Here is an idea ... you either implement the java.lang.Comparable interface and define the method compareTo(Object obj)
or make a new class whitch will implemet java.util.Comparator and it will implement a method comare(Object o1, Object o2). Then you could use it with other classe which can order your collection or array of classes. Here is an example of a class named Country with two private member variables, and it implements Comparable. Here is also a class which implements Comparator, it's name is CountryComparator and it can help you sort CountryClasses where you can tell it which column to order by (your country class probobly reprisents a table in a databace), in this case, the column country or code .

When you use Arrays.sort(Object[]) it will check if you implement the Object parameter implements Comparable and then your implementation of compareTo. The sorting function in Arrays then uses quick sort algorithm to order your array (rather then merge sort i think ...) good luck


public class Country implements java.lang.Comparable{


private int nCode;

private String strCountry;

public Country(int code, String country ) {

this.setCode(code);

this.setCountry(country);

}

public static void main(String[] args) {




//Here is how you use it ...


Country[] countryArr = new Country[4];

countryArr[0] = new Country(10, "Iceland");

countryArr[1] = new Country(9, "Sweeden");

countryArr[2] = new Country(17, "Norway");

countryArr[3] = new Country(1, "Danmark");



CountryComparator comparator = new CountryComparator(CountryComparator.COUNTRY);



Arrays.sort(countryArr, comparator);



// if you should use Arrays.sort(countryArr) with only this

// Arrays would call the method Comparable.compareTo(Object o)

// whitch is also defined in this class



for (int i = 0; i < countryArr.length; i++) {

String str = countryArr[i].getCode()+" "+countryArr[i].getCountry();

System.out.println(str);

}

//output ...

//1 Danmark

//10 Iceland

//17 Norway

//9 Sweeden





// you could also do ...

// Map m = new TreeMap(comparator);

// List l = new LinkedList()

// load the list with countries and do

// Collections.sort(list, comparator);

}



public int compareTo(Object o) {

Country c = (Country) o;

return this.getCountry().compareTo(c.getCountry());

}



public int getCode(){return nCode;}

public String getCountry(){return strCountry;}

public void setCountry(String country){this.strCountry = country;}

public void setCode(int code){this.nCode = code;}




}






public class CountryComparator implements java.util.Comparator {

//this option gives you a chanse to order by other elements in country

// such as code or something else you might have there

public final static int COUNTRY = 1;

public final static int CODE = 2;

private int columnToSortBy;



public CountryComparator(int column) {

columnToSortBy = column;

}


public int compare(Object o1, Object o2) {

Country c1 = (Country) o1;

Country c2 = (Country) o2;

int result;

if(columnToSortBy == COUNTRY)

result = c1.getCountry().compareTo(c2.getCountry());

else {//(columnToSortBy == CODE)

if(c1.getCode() > c2.getCode())

result = 1;

else if(c1.getCode() < c2.getCode())

result = -1;

else

result = 0;//probobly never happents!! still ...

}

return result;

}


}

Re: Displaying Countries in sorted order

Posted By:   John_Zukowski  
Posted On:   Sunday, October 28, 2001 04:21 PM

TreeMap already keeps items sorted.
About | Sitemap | Contact