4 kyu
Fancy Schmancy Sort
130 of 137osuushi
Loading description...
Arrays
Sorting
Object-oriented Programming
Algorithms
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
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).
bump
Done
You should add description for 'length' path
See below.
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'?
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.
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.
As kirilloid said, it sounds like you aren't handling the path.
How on Earth can we have
sortByPath(arr, path, [args...], [fn])
with variable count of args and optional argumentfn
after? Of course, there're strategies to check, whether to use it or not: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.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?
The test that includes "yadf" also passes a comparison function.
You should add description for 'length' path.
What is there to explain? Arrays have a
length
property. It's just a normal path.What an evil little kata.