David Sanford
David Sanford

Reputation: 745

Special SQLite sort order Swift 3

I want to have a sorted array of common names. They would be easily sorted in SQLite by

`...ORDER BY RL_Species.species ASC`

However, not every species has a common name, so there are many records with "no common names". I would like to sort all common names ASC, but have all of the "no common name" be on the bottom of the sorted list.

It is called in a TableViewController with the basic code:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell: UITableViewCell       = tableView.dequeueReusableCell(withIdentifier: Resource.SpeciesCell)!

    let specieCommonName: UILabel   =         
            speciesCommonName.text   = self.species[(indexPath as NSIndexPath).row].commonName


    return cell
}

Is this possible?

Upvotes: 0

Views: 189

Answers (1)

Vini App
Vini App

Reputation: 7485

Please try the below query :

SELECT * FROM `TABLENAME` ORDER BY case when RL_Species.species='no common name' then 1 else 0 end,RL_Species.species ASC

Upvotes: 2

Related Questions