Skip to content

Commit

Permalink
Merge pull request #251 from morganchen12/sorted-array
Browse files Browse the repository at this point in the history
fix sorting behavior in FUISortedArray
  • Loading branch information
morganchen12 authored Feb 24, 2017
2 parents b6f8ace + 3b64f67 commit aeda385
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions FirebaseDatabaseUI/FUISortedArray.m
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,16 @@ - (NSInteger)insertSnapshot:(FIRDataSnapshot *)snapshot {
}
}

NSInteger lowerBound = 0;
NSInteger upperBound = self.snapshots.count;
NSInteger index = self.count / 2;
while (index >= 0 && index <= self.count) {
while (index >= 0 && index <= upperBound) {

if (index == 0) {
[self.snapshots insertObject:snapshot atIndex:index];
[self.snapshots insertObject:snapshot atIndex:0];
return 0;
}
if (index == self.count) {
if (index == self.snapshots.count) {
[self.snapshots addObject:snapshot];
return index;
}
Expand All @@ -132,11 +135,13 @@ - (NSInteger)insertSnapshot:(FIRDataSnapshot *)snapshot {

if (left == NSOrderedDescending && right == NSOrderedAscending) {
// look left
index /= 2;
upperBound = index;
index = (lowerBound + upperBound) / 2;
continue;
} else if (left == NSOrderedAscending && right == NSOrderedDescending) {
// look right
index = ((self.count - index) / 2) + index + 1;
lowerBound = index + 1;
index = (lowerBound + upperBound) / 2;
continue;
} else if (left == NSOrderedDescending && right == NSOrderedDescending) {
// bad state (array is not sorted to begin with)
Expand Down

0 comments on commit aeda385

Please sign in to comment.