Search code examples
iosswiftparse-platformpfquery

iOS Parse OrderByAscending result wrong for Turkish Character


When i fetch data with "orderByAscending" from Parse, the result return wrong for Turkish Characters(ç,ö,ü,vs) and upper Characters.

All Turkish character ordered in the end of the result. Result example:

Current Result. This is wrong ---- Ali,Ceyda,Mehmet,Zeynep,Çan,Ömer

Expected Result. This is true ---- Ali,Ceyda,Çan,Mehmet,Ömer,Zeynep

Upper Characters example:

Current Result. This is wrong ---- BBC,Back,Bistro

Expected Result. This is true ---- Back,BBC,Bistro

My code below:

func getData(){



    let Query = PFQuery(className: "Table")
    Query.limit = 1000
    Query.orderByAscending("Name")

    Query.findObjectsInBackgroundWithBlock { (objects, error) in


        if error != nil {

            print(error)



        }else{


            for object in objects! {



                let name = object["Name"] as! String
                let link = object["Link"] as! String
                let logo = object["Logo"] as! PFFile
                let isPremium = object["isPremium"] as! Int
                let objectID = object.objectId




                let LogoUrl = logo.url



                Model.sharedInstance.items.addItem(name, link: link, logo: LogoUrl!, isPremium: isPremium,objectID:objectID!)


            }



            NSOperationQueue.mainQueue().addOperationWithBlock({

                self.tableView.reloadData()


            })


        }



    }

}

How can i handle this situation?

Thanks.


Solution

  • You could manually sort the objects array returned by the (potentially flawed) server backend:

    Query.findObjectsInBackgroundWithBlock { (objects, error) in
    
        if error != nil {
            print(error)
        } else {
    
            // sort the objects by hand
            let sortedObjects = objects!.sort {         // in Swift 3, use "sorted" instead
                 let nameA = $0["Name"] as! String
                 let nameB = $1["Name"] as! String
    
                 return nameA < nameB
           }
    
            for object in sortedObjects {
                   // do your work with the ordered objects
            }
            NSOperationQueue.mainQueue().addOperationWithBlock({
                self.tableView.reloadData()
            })
        }
    }
    

    Note that he code I provided passes off the ordering to the standard Swift String comparison operator ('<'). You could also manually sort the query results using other methods as shown in this question.