2

I have an strng array.

e.g:

StrArray = ["hook", "hour", "javascript", "nemo", "case"];

I know that if I use StrArray.Sort() it will sorted alphabetical. But I would to like to sort by using this alphabet = "jngmclqskrzfvbwpxdht"

I've searched but I only find people using HashTable to solve it.

Is it possible to do in JS?

1
  • the sort function, takes an optional function. SO you can say: list.sort(function(a,b) { return a < b; });. Therefore if you want to customize the ability to sort, you would define the parameters in there. Commented Jun 19, 2018 at 3:48

3 Answers 3

4

let order = 'jngmclqskrzfvbwpxdht'
let StrArray = ["hook", "javascript", "hour", "nemo", "case"];

StrArray.sort(function(a,b) {
  let firstAlphaOfParam1 = a.charAt(0);
  let firstAlphaOfParam2 = b.charAt(0);
  
  return order.indexOf(firstAlphaOfParam1) - order.indexOf(firstAlphaOfParam2); 
  
})
console.log(StrArray);

The solution is only taking into consideration of sorting by only the first alphabet of element in StrArray. Basically we take the first alphabet and find the index in your jngmclqskrzfvbwpxdht and compare them

Sign up to request clarification or add additional context in comments.

6 Comments

Nice! but and if I had a more word "hour" ?
Never return only true false in a sorter, you will only move forward elements, never backward (-1) => it won't get sorted.
@Kaiido: I can't really understand your point, do you mind to further elaborate?
@LeonardoLima: As stated, the sorting only compare first alphabets, so hour and hook will be sorted to side by side.
The three expected returned values for the sort's function param are -1 (move forward) 0 (don't move) and 1 (move backward). By returning only true and false values, you are limiting your algo to 0 and 1, and the sorting can't be done correctly since no item will be able to move forward.
|
2

Your sort vocabulary doesn't contain all the letters in you words, so it's not completely clear how to proceed with words like 'hour' and 'hook' as there's no 'o' in the sort list. You can just ignore them and treat anything that isn't in the list as equal in the sort order. You also should test against similar bases like "hook" and "hooks"

For example:

let StrArray = ["hook", "javascript", "nemo", "hours", "case", "hour", "houn"];
const sort_order = "jngmclqskrzfvbwpxdht"

StrArray.sort((a, b) => {
    let i = 0;
    while (i < a.length && i < b.length ){
        let a_i = sort_order.indexOf(a[i]),
            b_i = sort_order.indexOf(b[i]);
        if (a_i === b_i ) {
            i++
            continue
        }
        return a_i - b_i
    }
    // one is a substring of the other, sort by length
    return a.length - b.length
})

console.log(StrArray)

Comments

1

I can leave a small contribution, this code can sort using the first letter.

var arr1 = ["hook", "javascript", "nemo", "case"];
var myAbc = 'jngmclqskrzfvbwpxdht';
var final_array = [];

for (i = 0; i < myAbc.length; i++) {
  for (j = 0; j < arr1.length; j++) {        
    if (arr1[j].charAt(0) == myAbc.charAt(i)) {        
      final_array.push(arr1[j]);      
    }
  }      
};

console.log(final_array);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.