0

I've got a sqlite table with sub_id's, these are stored as strings. When I order with this column, I get my data ordered like this:

1, 10, 13, 16, 2, 23, etc

How can I order them numerically?

My code:

var id = Expression<Int>("id")
let m_id = Expression<String>("m_id")

let chapters = try self.database.prepare(self.chaptersTable.order(self.m_id))

for chapter in chapters {
    print("chapter Id: \(chapter[self.m_id])")
    idsChapters.append(chapter[self.id])
    mIdChapters.append(chapter[self.m_id])
}

I can't use change may database format due to Android compatibility.

Albert-Jan
  • 315
  • 1
  • 3
  • 11

1 Answers1

0

Can't test this at the moment, but try:

let chapters = try self.database.prepare(self.chaptersTable.order(CAST(self.m_id AS INTEGER)))

Gleaned from the SO question Compare strings as numbers in SQLite3, comment by @Martin Ždila

leanne
  • 7,940
  • 48
  • 77