0

I have been thinking for quite some time now on how to sort my array in Javascript in the way I want to, trying various methods and I can't come up with a solution so I decided to ask here... So in a nutshell, my array looks as complex as that:

var englishvoc = new Array();

englishvoc[0] = new Array();
englishvoc[0][0] = "beautiful";
englishvoc[0][1] = new Array();
englishvoc[0][1][0] = 0;
englishvoc[0][2] = 2;

englishvoc[1] = new Array();
englishvoc[1][0] = "nice";
englishvoc[1][1] = new Array();
englishvoc[1][1][0] = 0;
englishvoc[1][2] = 2;

englishvoc[2] = new Array();
englishvoc[2][0] = "get";
englishvoc[2][1] = new Array();
englishvoc[2][1][0] = 9;
englishvoc[2][1][1] = 7;
englishvoc[2][1][2] = 2;
englishvoc[2][2] = 1;

The way I want this array to be sorted is just by the English word in there, so that the order of them become "beautiful", "get", "nice", while moving the whole first dimensions of the array. Result should be:

alert(englishvoc) //Returns: '"beautiful",0,2,"get",9,7,2,1,"nice",0,2'

In addition to that, it would be great if this sorting system would not be case sensitive... Maybe someone has a solution for that complicated array sorting? Thanks in advance!

Lucia
  • 19
  • 3
  • can you give a more generalized explanation of your problem ? so that we can give you a solution that works in all cases – Stakvino Aug 25 '18 at 12:32
  • in this example we go from englishvoc[0] to englishvoc[7] and then to englishvoc[2] but in the general case how do you chose the order ? – Stakvino Aug 25 '18 at 12:34
  • Regardless of what else the array includes, it should be sorted by englishvoc[(part to be sorted)][0]... – Lucia Aug 25 '18 at 12:35
  • Hi! *"trying various methods and I can't come up with a solution..."* What did your various attempts look like? If you show us, then we can help you fix whatever's wrong with them. – T.J. Crowder Aug 25 '18 at 12:35
  • The order is chosen by those English words. – Lucia Aug 25 '18 at 12:36
  • @T.J.Crowder I'll show you later – Lucia Aug 25 '18 at 12:36
  • @Lucia - Then please delete the question, and then edit it and undelete it when you're ready. – T.J. Crowder Aug 25 '18 at 12:37
  • 1
    @T.J. Crowder ahh sorry, I just copied a few examples out of my array and forgot to replace that 2 by 1 and 7 by 2. In my actual program there are no holes... I'm sorry. (will edit my question) - btw, I'll try the answers when I'm at home – Lucia Aug 25 '18 at 14:23

2 Answers2

0

You could try sort() and localeCompare()

var englishvoc = new Array();

englishvoc[0] = new Array();
englishvoc[0][0] = "beautiful";
englishvoc[0][1] = new Array();
englishvoc[0][1][0] = 0;
englishvoc[0][2] = 2;

englishvoc[2] = new Array();
englishvoc[2][0] = "nice";
englishvoc[2][1] = new Array();
englishvoc[2][1][0] = 0;
englishvoc[2][2] = 2;

englishvoc[7] = new Array();
englishvoc[7][0] = "get";
englishvoc[7][1] = new Array();
englishvoc[7][1][0] = 9;
englishvoc[7][1][1] = 7;
englishvoc[7][1][2] = 2;
englishvoc[7][2] = 1;



englishvoc.sort((a,b)=>a[0].localeCompare(b[0]))

console.log(englishvoc.join())
Emeeus
  • 5,072
  • 2
  • 25
  • 37
  • Thanks a lot! That's exactly what I needed and I didn't think it's that simple... – Lucia Aug 25 '18 at 15:59
  • Your welcome, if you'd have special characters, see this https://stackoverflow.com/a/51897069/6121568 – Emeeus Aug 25 '18 at 16:10
-1
To me it looks like you are trying to represent an object array as multi dimensional array.  I think you should be able to represent like below.  


[{
    name:  "beautiful";
    data: {
            val1: [0]
            val2: [1]
        }
    },
    {
    name:  "nice";
    data: {
            val1: [0]
            val2: [1]
        }
    },
    {
    name:  "get";
    data: {
            val1: [9]
            val2: [7,2]
        }
}]

This array of object should be easily sortable.