Skip to content Skip to sidebar Skip to footer

Nth Longest String Sortation

I have written code for determining the nth longest string in an array of strings. Below I have test cases listed from the Codewars kata. Instructions: Implement the function longe

Solution 1:

Works fine on MSIE.

A quick test on Microsoft Internet Explorer (any version) gives the following results for the function you provided:

>> longest(['a','b','c','d','e','f','g','h','i','k'],1); 
 a,b,c,d,e,f,g,h,i,k 
"a" 
>> console.log(longest(['a','b','c','d','e','f','g','h','i','k','l'],1)); 
 a,b,c,d,e,f,g,h,i,k,l 
 a 
>> console.log(longest(['a','b','c','d','e','f','g','h','i','k','l',"m","n"],1)); 
 a,b,c,d,e,f,g,h,i,k,l,m,n 
 a

p.s.: All non MS browsers have problems with the stability of sort().

Solution 2:

You use the built-in sort function, maybe this function change the algorithm of sort depending of your array ending up by not having the same behaviour with same length strings. And maybe even this is browser dependant.

I suggest you to change this by using a library with a determined sort function (quicksort, whatever...). And check if that happen again.

Post a Comment for "Nth Longest String Sortation"