Bad*_*Boy 1 sqlite sql-order-by
我正在使用SQLite查询(在iOS应用程序中),如下所示:
SELECT * FROM tblStations WHERE StationID IN ('206','114','113','111','112','213','214','215','602','603','604')
Run Code Online (Sandbox Code Playgroud)
但是,我正在以降序或升序获得结果数据,而我真正想要的是按照我在IN子句中指定的顺序返回数据.
这可能吗?
对结果进行排序的一种简单方法
NSArray *stationIDs = @[@206,@114,@113,@111,@112,@213,@214,@215,@602,@603,@604];
NSArray *stations = @[@{@"Id":@(604)},@{@"Id":@(603)},@{@"Id":@(602)},@{@"Id":@(215)},
@{@"Id":@(214)},@{@"Id":@(213)},@{@"Id":@(112)},@{@"Id":@(111)},
@{@"Id":@(113)},@{@"Id":@(114)},@{@"Id":@(206)}];
stations = [stations sortedArrayUsingComparator:
^NSComparisonResult(NSDictionary * dict1, NSDictionary *dict2)
{
NSUInteger index1 = [stationIDs indexOfObject:dict1[@"Id"]];
NSUInteger index2 = [stationIDs indexOfObject:dict2[@"Id"]];
return [@(index1) compare:@(index2)];
}];
Run Code Online (Sandbox Code Playgroud)