Last active
March 21, 2018 15:52
-
-
Save MaxGraey/e082490e8078cca611d0c9510a31a8c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function defaultComparator<T>(a: T, b: T): i32 { | |
return (<i32>(a > b)) - (<i32>(a < b)); | |
} | |
export function createDefaultComparator<T>(): (a: T, b: T) => i32 { | |
return (a: T, b: T): i32 => (<i32>(a > b)) - (<i32>(a < b)); | |
} | |
export function isSorted<T>(data: Array<T>, comparator: (a: T, b: T) => i32 = createDefaultComparator<i32>()): bool { | |
for (let i: i32 = 1, len: i32 = data.length; i < len; i++) { | |
if (comparator(data[i - 1], data[i]) > 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
isSorted<i32>([1, 2, 3, 4]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment