1

I have below code to get data from SQLite database in android.

SystemCountryListDS system_country_list_ds;
system_country_list_ds.open();
List<SystemCountryList> system_country_list = system_country_list_ds.findAll();

I want to get this list in ascending order by country name. How can I do that?

I tried to do this on list:

Collections.sort(system_country_list);

It's giving me error:

inferred type is not within its bound; should implement java.lang.comparable

user2864740
  • 60,010
  • 15
  • 145
  • 220
RNK
  • 5,582
  • 11
  • 65
  • 133
  • you need to provide a comparator object to sort as second parameter where you specify how the method will sort the dataset – Blackbelt Mar 19 '15 at 15:11
  • What happens you *search* for the error? Because the error says what it means: SystemCountryListDS does not implement Comparable; see the Collections.sort documentation for why it is required. (This exception has *nothing* to do with SQLite, but if the can be / is ordered in SQLite then you wouldn't need to do so in Java.) – user2864740 Mar 19 '15 at 15:11
  • http://developer.android.com/reference/java/lang/Comparable.html , http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/ , http://stackoverflow.com/questions/20204667/java-generics-inserting-inner-type-parameter – user2864740 Mar 19 '15 at 15:14
  • possible duplicate of [Collections.sort in java for any class](http://stackoverflow.com/questions/6830807/collections-sort-in-java-for-any-class) – Philipp Wendler Mar 19 '15 at 18:33

4 Answers4

0

it means SystemCountryListDS don't implements Comparable interface. There is Collections.sort where you can pass a Comparator Collections.sort with Comporator

You can do

Collections.sort(system_country_list, new Comparator<SystemCountryListDS>() {
            @Override public int compare(SystemCountryListDS  o1, SystemCountryListDS  o2) {
                if (o1 == o2)
                    return 0;
                if (o1 == null)
                    return -1;
                if (o2 == null)
                    return 1;

                String country1  = o1.getCountry();
                String country2  = o2.getCountry();

                if (country1 == country2)
                    return 0;
                if (country1 == null)
                    return -1;
                if (country2 == null)
                    return 1;
                return country1.compareTo(country2);
            }
        });
Vyncent
  • 1,185
  • 7
  • 17
0

This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.

So, what the docs tell you is that whatever class you want to sort should implement the Comparable interface so that it becomes comparable. The compareTo method will be used to compare the two objects.

If, however, you do not want to implement Comparable, you can create a new class that implements Comparator interface.

A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order.

An SO User
  • 24,612
  • 35
  • 133
  • 221
0

Your Class should implements Comparable<>. See here for a detailed example.

Community
  • 1
  • 1
Mel
  • 453
  • 1
  • 4
  • 14
0

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());
    }
});
Naval Kishor Jha
  • 894
  • 9
  • 13