What is the best way to compare two lists in Javascript as you would do in Python? -
What is the best way to compare two lists in Javascript as you would do in Python? -
i'm quite new javascript familiar python. in python output:
in [1]: [1,9,[5,4,2]] > [1,9,[14,5,4]] out[1]: false
in javascript:
> [1,9,[5,4,2]] > [1,9,[14,5,4]] true
it seems arrays converted string before comparison.
now wanted write function myself , walk through array compare each element. came coffeescript code:
compare_list = (a, b)-> if typeof == "object" , typeof b != "object" homecoming 1 else if typeof != "object" , typeof b == "object" homecoming -1 else if typeof != "object" , typeof b != "object" if > b homecoming 1 else if < b homecoming -1 else homecoming 0 else if typeof == "object" , typeof b == "object" in [0...a.length] if > (b.length-1) homecoming 1 tmp = compare_list a[i], b[i] if tmp != 0 homecoming tmp if b.length > a.length homecoming -1 homecoming 0
it works way typeof == "object"
part looks not right me. there simpler/better/more robust solution?
thanks help.
trying solve same issue, came custom solution. https://gist.github.com/ruxkor/2772234
as javascript using string coercion when comparing object, think necessary utilize custom comparing function simulate python behavior.
javascript python optimization coffeescript
Comments
Post a Comment