4 kyu

Fancy Schmancy Sort

130 of 137osuushi
Description
Loading description...
Arrays
Sorting
Object-oriented Programming
Algorithms
  • Please sign in or sign up to leave a comment.
  • Voile Avatar

    CS version is broken as of https://github.com/Codewars/codewars.com/issues/1315#issuecomment-375152784.

    Someone should fix this according to https://github.com/Codewars/codewars-runner-cli/issues/406. (I'll fix this after this issue has existed for a week, but someone with edit rights could do it before me).

  • donurg Avatar

    You should add description for 'length' path

  • hpstuff Avatar

    How can I know when to apply last function parameter

    array [ 'ab', 'x', 'abcdefghijklmnop' ] with path 'length' and function 'str'

    Expected: ["x","abcdefghijklmnop","ab"], instead got: ["x","ab","abcdefghijklmnop"]

    What if I get [{a: {b: "x"}}, {a: {b: "abcdefghijklmnop"}}, {a: {b: "ab"}] with path "a.b.length" and function 'str'?

  • db222 Avatar

    The test set is broken, in particalur: "Should handle custom sorting Expected: ["yadf","abc","d"], instead got: ["d","abc","yadf"]" The custom sorting function given is as follows; "function (a, b) { return b - a; }" However the array is a set of strings, so all this returns is NaN.

    • kirilloid Avatar

      You're missing a point. Even with custom sorting function you still need to apply the path to the objects. Check all arguments coming to your function.

    • osuushi Avatar

      As kirilloid said, it sounds like you aren't handling the path.

      Issue marked resolved by osuushi 9 years ago
  • kirilloid Avatar

    How on Earth can we have sortByPath(arr, path, [args...], [fn]) with variable count of args and optional argument fn after? Of course, there're strategies to check, whether to use it or not:

    • get the last element from the args array and check it is 'num', 'str' or function.
    • get the last element from the args array only if it's not used in path. But why should we guess?
    • osuushi Avatar

      You have to count the number of arguments specified in the path to figure out how many arguments you need to accept. So for example, if the path was "foo(2).bar(1)", you count up the total arguments, and find that the next 3 arguments will get passed. If there is another argument left over after that, you use it as the optional function.

    • Voile Avatar
      Issue marked resolved by Voile 8 years ago
  • Enjeru Avatar

    I have question, at first test case I got these arguments: {"0":["ab","xyzc","x"],"1":"length"} and expected result is ["x","ab","xyzc"] and it's ok to understand. But at "Should handle custom sorting" test case I got "0":["yadf","abc","d"],"1":"length"} and expected result is ["yadf","abc","d"]. In both cases I need to sort by length, but in first one I need to sort by ASC, nd in second one I need sort by DESC. How could I detect in which case I need to sort?

  • zair Avatar

    You should add description for 'length' path.

  • jacobb Avatar

    What an evil little kata.