Skip to content

Data retrieval with Sorting

Data retrieval and search API can request the server to return sorted data. Data can be sorted by one or more columns. The sorting direction can be either ascending (default) or descending for each individual column. iOS applications must use the DataQueryBuilder class to set the sorting option. The example below configures DataQueryBuilder to sort results by the name and age columns. Value in the age column are sorted in the descending order. The query is used to retrieve objects from the Person table:

DataQueryBuilder *queryBuilder = [DataQueryBuilder new];
[queryBuilder setSortBySortBy:@[@"name", @"age DESC"]];

DataStoreFactory *dataStore = [Backendless.shared.data of:[Person class]];
[dataStore findWithQueryBuilder:queryBuilder responseHandler:^(NSArray *sortedObjects) {
    NSLog(@"Sorted objects: %@", sortedObjects);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let queryBuilder = DataQueryBuilder()
queryBuilder.setSortBy(sortBy: ["name", "age DESC"])

let dataStore = Backendless.shared.data.of(Person.self)
dataStore.find(queryBuilder: queryBuilder, responseHandler: { sortedObjects in
    print("Sorted objects: \(sortedObjects)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

DataQueryBuilder *queryBuilder = [DataQueryBuilder new];
[queryBuilder setSortBySortBy:@[@"name", @"age DESC"]];

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Person"];
[dataStore findWithQueryBuilder:queryBuilder responseHandler:^(NSArray *sortedObjects) {
    NSLog(@"Sorted objects: %@", sortedObjects);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let queryBuilder = DataQueryBuilder()
queryBuilder.setSortBy(sortBy: ["name", "age DESC"])

let dataStore = Backendless.shared.data.ofTable("Person")
dataStore.find(queryBuilder: queryBuilder, responseHandler: { sortedObjects in
    print("Sorted objects: \(sortedObjects)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Important

The descending sorting order can be requested by adding DESC to the name of the column (separated by space).