You can get the country name as you requiered asc or desc while fetching the data from the table of your database in sqlite
like using query statement or rawQuery
query( Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit))-->
SqliteDBObject.query(tableName,new String[] {ColumnNameOfCountry},null,null,null,null,ColumnNameOfCountry+" asc",null);
or
by Cursor rawQuery (String sql, String[] selectionArgs)--->
SqliteDBObject.rawQuery ("select ColumnNameOfCountry from tableName order by ColumnNameOfCountry asc ", null);
this both query will return you the cursor which is having the column ColumnNameOfCountry
with data as ascending order or if you want to your data in descending order so replace the word asc
in both the query to DESC
and you will get it.
and use this cursor to take out the data in SystemCountryListDS
class by doing the operation in findAll()
and add to list and return it to set to the adapter or where ever you want to use it.
other way is the way you are try like sorting using Collections
class but the function you have used is Collections.sort(system_country_list);
is you for the only those class which has implement the Comparable
interface. it will sort in natural order by default (ascending only) as String has implemented this interface so you can use this function and without any problem you will get the result you want.
but in your case
you can achieve the same in two way either implement the Comparable interface or by using your own Comparator class by implementing comparator interface
example of comparable is (in your case )
class SystemCountryList implements Comparable<SystemCountryList>
{
/*All your field or variable and member function will go here */
/** i m assuming that you country name will varible or field is
country
and you have used the getter and setter function for it
if not below i am writing
**/
private String country;
public void setCountry(String country)
{
this.country=country;
}
public String getCountry()
{
return country;
}
/*
** Implement the natural order for this class
*/
public int compareTo(SystemCountryList s)
{
return getCountry().compareTo(s.getCountry());
}
}
now our code will work
Collections.sort(system_country_list);
the other way is making your own comparator call
Collections.sort(system_country_list, new Comparator<SystemCountryList>() {
@Override
public int compare(SystemCountryList s1, SystemCountryList s2) {
return (s2.getCountry()).compareTo(s1.getCountry());
}
});