mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-15 11:13:50 +00:00
New: Sort using descriptors handles nil and CPNull in correct way
This is the same way as Cocoa works. Also added test case.
This commit is contained in:
1 parent
fc403d679c
commit
f52f11899a
2 files changed
+35
-2
No files matched your search
@@ -429,6 +429,9 @@ var sortArrayUsingFunction = function(array, aFunction, aContext)
|
||||
}
|
||||
}
|
||||
|
||||
// This is for speed
|
||||
var CPMutableArrayNull = [CPNull null];
|
||||
|
||||
// Observe that the sort descriptors has the reversed order by the caller
|
||||
var sortArrayUsingJSDescriptors = function(a, d)
|
||||
{
|
||||
@@ -451,7 +454,10 @@ var sortArrayUsingJSDescriptors = function(a, d)
|
||||
aUID,
|
||||
bUID,
|
||||
key,
|
||||
dd;
|
||||
dd,
|
||||
value1,
|
||||
value2,
|
||||
cpNull = CPMutableArrayNull;
|
||||
|
||||
if (dl < 0)
|
||||
return;
|
||||
@@ -519,7 +525,14 @@ var sortArrayUsingJSDescriptors = function(a, d)
|
||||
{
|
||||
dd = d[cn];
|
||||
key = dd.k;
|
||||
o = objj_msgSend(C1[key], dd.s, C2[key]);
|
||||
value1 = C1[key];
|
||||
value2 = C2[key];
|
||||
if (value1 === nil || value1 === cpNull)
|
||||
o = value2 === nil || value2 === cpNull ? CPOrderedSame : CPOrderedAscending;
|
||||
else
|
||||
{
|
||||
o = value2 === nil || value2 === cpNull ? CPOrderedDescending : objj_msgSend(value1, dd.s, value2);
|
||||
}
|
||||
|
||||
if (o && !dd.a)
|
||||
o = -o;
|
||||
|
||||
@@ -470,6 +470,26 @@
|
||||
[self assert:[5, 4, 4, 3, 2, 2, 1, 1, 1, 1] equals:target];
|
||||
}
|
||||
|
||||
- (void)testThatCPArrayDoesSortCorrectWithNilAndCPNull
|
||||
{
|
||||
var descriptors = [[CPSortDescriptor sortDescriptorWithKey:@"number" ascending:NO]],
|
||||
target = [
|
||||
[[CPPrettyObject alloc] initWithValue:@"a" number:nil],
|
||||
[[CPPrettyObject alloc] initWithValue:@"a" number:[CPNull null]],
|
||||
[[CPPrettyObject alloc] initWithValue:@"a" number:@"Objective-J"],
|
||||
[[CPPrettyObject alloc] initWithValue:@"a" number:[CPNull null]],
|
||||
[[CPPrettyObject alloc] initWithValue:@"a" number:nil],
|
||||
];
|
||||
|
||||
[target sortUsingDescriptors:descriptors];
|
||||
|
||||
[self assert:@"Objective-J" equals:[target[0] number]];
|
||||
[self assert:nil equals:[target[1] number]];
|
||||
[self assert:[CPNull null] equals:[target[2] number]];
|
||||
[self assert:[CPNull null] equals:[target[3] number]];
|
||||
[self assert:nil equals:[target[4] number]];
|
||||
}
|
||||
|
||||
- (void)testMutableCopy
|
||||
{
|
||||
var normalArray = [],
|
||||
|
||||
Reference in new issue
Block a user