From 8a30bf5705c4910dc03b17301ecd7faa7bd79d94 Mon Sep 17 00:00:00 2001 From: Bhanu Pratap Singh Rathore Date: Mon, 10 Dec 2018 12:32:20 +0530 Subject: [PATCH] feat(interview-prep): Porting Rosetta Problem: Sort custom (#31160) --- .../sort-using-a-custom-comparator.md | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sort-using-a-custom-comparator.md diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sort-using-a-custom-comparator.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sort-using-a-custom-comparator.md new file mode 100644 index 0000000000..07e086a561 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sort-using-a-custom-comparator.md @@ -0,0 +1,67 @@ +--- +id: 5a23c84252665b21eecc8016 +title: Sort using a custom comparator +challengeType: 5 +--- + +## Description +
+Write a function to sort an array (or list) of strings in order of descending length, and in ascending lexicographic order for strings of equal length. +
+ +## Instructions +
+ +
+## Tests +
+ +``` yml +tests: + - text: lengthSorter should be a function. + testString: assert(typeof lengthSorter == 'function', 'lengthSorter should be a function.'); + - text: lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]) should return a array. + testString: assert(Array.isArray(lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])), 'lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]) should return a array.'); + - text: lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]) should return ["strings", "sample", "sorted", "Here", "some", "are", "be", "to"]. + testString: assert.deepEqual(lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]), ["strings", "sample", "sorted", "Here", "some", "are", "be", "to"], 'lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]) should return ["strings", "sample", "sorted", "Here", "some", "are", "be", "to"].'); + - text: lengthSorter(["I", "hope", "your", "day", "is", "going", "good", "?"]) should return ["going", "good", "hope", "your", "day", "is", "?","I"]. + testString: assert.deepEqual(lengthSorter(["I", "hope", "your", "day", "is", "going", "good", "?"]), ["going", "good", "hope", "your", "day", "is", "?","I"], 'lengthSorter(["I", "hope", "your", "day", "is", "going", "good", "?"]) should return ["going", "good", "hope", "your", "day", "is", "?","I"].'); + - text: lengthSorter(["Mine", "is", "going", "great"]) should return ["going", "great", "Mine", "is"]. + testString: assert.deepEqual(lengthSorter(["Mine", "is", "going", "great"]), ["going", "great", "Mine", "is"], 'lengthSorter(["Mine", "is", "going", "great"]) should return ["going", "great", "Mine", "is"].'); + - text: lengthSorter(["Have", "fun", "sorting", "!!"]) should return ["sorting", "Have", "fun", "!!"]. + testString: assert.deepEqual(lengthSorter(["Have", "fun", "sorting", "!!"]), ["sorting", "Have", "fun", "!!"], 'lengthSorter(["Have", "fun", "sorting", "!!"]) should return ["sorting", "Have", "fun", "!!"].'); + - text: lengthSorter(["Everthing", "is", "good", "!!"]) should return ["Everthing", "good", "!!", "is"]. + testString: assert.deepEqual(lengthSorter(["Everthing", "is", "good", "!!"]), ["Everthing", "good", "!!", "is"], 'lengthSorter(["Everthing", "is", "good", "!!"]) should return ["Everthing", "good", "!!", "is"].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function lengthSorter (arr) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function lengthSorter(arr) { + arr.sort(function (a, b) { + var result = b.length - a.length; + if (result == 0) + result = a.localeCompare(b); + return result; + }) + return arr; +} +``` + +