How can I sort a string array using a specific alphabet?

java sort string array alphabetically
java sort set of strings alphabetically
java sort string characters alphabetically
java sort string array alphabetically without using sort method
sort string in alphabetical order
java code to arrange the letters of a word in alphabetical order
how to check if a string is in alphabetical order in java
arrange sentence in alphabetical order in java

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?

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)

List an Array of Strings in alphabetical order, Weird, your code seems to work for me: import java.util.Arrays; public class Test { public static void main(String[] args) { // args is the list of guests  The sort function makes changes in the original array only. When we do not pass any third argument then by default it gets sorted in ascending order. We are using a user-defined comparator here.

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);

Sort the array of strings according to alphabetical order defined by , Given a string str and an array of strings strArr[], the task is to sort the array and store the value in a map with character as the key and its index in the array as  There are some predefined functions to make your work easier but we can always do that without using them. Take the array of strings and sort that array based only on first character. Once you are done then take the array of strings again and sort it based on second character.

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);

Java Program to Sort Strings in an Alphabetical Order, Once the count is captured using Scanner class, we have initialized a String array of the input count size and then are running a for loop to capture all the strings  1. Sorting string array alphabetically – String.compareTo() We are going to write a program which will work ask for number of strings, user want to sort. Then user need to enter each string one at a time and press ENTER after each input.

Beginning Java - Unit 6 Arrays - Alphabetic Sorting, Sorting into alphabetic order can be accomplished with both the bubble and sorting alphabetically is the way in which Java deals with comparing String values  and it is the last element i specifically want. The following program first extract the last integer part of your string element. And then return by comparing them.

Sort String Array alphabetically by the initial character only in Java, Here, we are sorting string array alphabetically by the initial character i.e. 'J' for '​John' will come after 'Chris' since the first character of Sort the array A-Z. I like to use a Function, rather than a Sub for sorting an array. The function is a reusable piece of code, it can form part of your ‘Core’ module, which you can copy into any VBA project. This function takes an array as its variable and outputs the same array sorted in alphabetical order.

How to Sort a String in Java alphabetically in Java?, Convert the given string to a character array using the toCharArray() method. Sort the obtained array using the sort() method of the Arrays class. You can use this with characters or words that are stored inside your array. It even works with numbers by moving those ahead of character strings of text. In case you need to go in the opposite direction, I've added a second VBA macro that will sort in reverse alphabetical order.

Comments
  • 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.
  • 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.