sandar win
sandar win

Reputation: 13

Sort order in content provider

I created "book" table.

db.execSQL("CREATE TABLE IF NOT EXISTS `book` (`bcode` TEXT, `author_name` TEXT, `book_name` TEXT, `price` TEXT); ");

Then, I put my data like this.

" insert into book values ('1', 'ေညး', 'တခါက',  '၁၀၀' ); ",
  " insert into book values ('2', 'မင္းတေခတ္', 'သူသာ', '၁၅၀' ); ", 
  " insert into book values ('3', 'မင္းတေခတ္', 'သူသာ', '၁၀၀' ); ", 
  " insert into book values ('4', 'ေညး', 'တခါက', '၁၀၀' ); ", 
  " insert into book values ('5', 'ေညး', 'တခါက',  '၁၀၀' ); ", 
  " insert into book values ('6', 'လြန္းထားထား', 'တခါက', '၁၀၀' ); ", 
  " insert into book values ('7', 'အၾကည္ေတာ္', 'သံုည', '၁၀၀'); ", 
  " insert into book values ('9', 'ေညး', 'တခါက',  '၁၀၀'); ", 
  " insert into book values ('10', 'ေညး', 'တခါက',  '၁၀၀'); ", 
  " insert into book values ('11', 'ေညး', 'တခါက',  '၁၀၀'); ",};

when i sort first column "bcode" by 'ASC' , the output is like this.

'1', 'ေညး', 'တခါက',  '၁၀၀' 
'11', 'ေညး', 'တခါက',  '၁၀၀'
'2', 'မင္းတေခတ္', 'သူသာ', '၁၅၀'
.
.

I want to get order like in this form.

'1', 'ေညး', 'တခါက',  '၁၀၀' 
'2', 'မင္းတေခတ္', 'သူသာ', '၁၅၀'
.
.
.
'11', 'ေညး', 'တခါက',  '၁၀၀'

How can I sort?

Upvotes: 1

Views: 206

Answers (2)

dmSherazi
dmSherazi

Reputation: 3831

It is so because you are declaring bcode as TEXT, change it to INTEGER

Being Text it is sorted char by char and hence anything starting with 1 will always be displayed before the one that starts with 2 if you are sorting by ascending

Another approach that you can use if you insist to use TEXT for type of bcode is that you fix the maximum length of bcode and use leading zeros. If you decided 4 as the length so your bcodes should look like 0001,0002,0003 ... 0011,0012...1111 ...

Upvotes: 2

Milad Faridnia
Milad Faridnia

Reputation: 9477

your bcode type is TEXT just change your data type to INTEGER

Upvotes: 1

Related Questions