jeudi 26 février 2015

Mix Sort order Android SQLlite

I have a Table ProductTable in which products are listed by 3 types


product_Name, product_Type and product_Year


Lets say i have 10 products



A - 2014 , 2015
B - 2014 , 2015
C - 2014 , 2015
D - 2014 , 2015
E - 2014 , 2015

Product A B C are of type 1 and D and E are of type 2

When i sort using year desc i get 2015 products and then 2014 products(expected behaviour)

when i sort using name and year desc i get A 2015, A2014, B2015, B2014 and so on (expected behaviour)

When i sort using type i get
B 2014 ,
B 2015 ,
C 2014 ,
C 2015 ,
A 2014 ,
A 2015 ,
E 2014 ,
E 2015 ,
D 2014 ,
D 2015 .


the query i use for sorting is



SELECT * FROM Mytable WHERE productActive=? ORDER BY product_type


Required scenario is I want same order as above (B,C,A,E,D) but the 2015 product should come before the 2014



eg. B 2015 ,
B 2014 ,
C 2015 ,
C 2014 ,
A 2015 ,
A 2014 ,
E 2015 ,
E 2014 ,
D 2015 ,
D 2014 .


i Tried to use group by year or name but that is not giving the desired result.


Also tried adding product_year DESC but it was giving all 2015 first and then 2014 .


Any Inputs ?


Aucun commentaire:

Enregistrer un commentaire