0

I have got an array that contains data in hierarchical form such as:

Level 2
  chapter 1
  chapter 2
Level 4
  chapter 1
  chapter 2
Level 1
  chapter 1
  chapter 2
Level 3
  chapter 1
  chapter 2

If I just call array.sort(), the hierarchy gets disturbed. So, I have to develop my own way of sorting items. The thing I can't understand is, how would I compare two levels such that I would know that level 1 is less than level 2 and it should be at the top index of the array?

8
  • 2
    I have no idea what your array looks like... Commented Sep 12, 2011 at 14:41
  • How does this array get formed? Is it really an Array or just an Object Commented Sep 12, 2011 at 14:41
  • 1
    is your array flat? like ['level2', 'chapter1', 'chater2', 'level4'] etc Commented Sep 12, 2011 at 14:41
  • Does this mean arrays in arrays? Commented Sep 12, 2011 at 14:41
  • No this is an array that contains these strings. array[0]=level 2, array[1]=chapter 1, array[3]=level 4 and so on...so its a flat array!! Commented Sep 12, 2011 at 14:42

2 Answers 2

4

You really shouldn't be using a flat array. You lose all the hierarchical information. Something like this would be better:

//I've deliberately made these unsorted to show you that sorting works
levels = ["Level 4", "Level 3", "Level 1", "Level 2"];

data = {
  "Level 3" : ["chapter 1", "chapter 2"],
  "Level 1" : ["chapter 2", "chapter 1"],
  "Level 2" : ["chapter 2", "chapter 1"],
  "Level 4" : ["chapter 1", "chapter 2"]  
};

levels.sort();
for(var i = 0 i < levels.length; i++) {
    console.log(levels[i]);
    var chapters = data[levels[i]];

    chapters.sort();    
    for(var j = 0; j < chapters.length; j++) {
        console.log(chapters[j]);
    }
}

EDIT

Rob suggested using levels.sort(function(x,y){return x.localeCompare(y)}) instead of the regular .sort(). The former will sort ["abc", "Abcd", "Ab"] to ["Ab", "abc", "Abcd"] instead of ["Ab", "Abcd", "abc"].

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

3 Comments

Your extra comma in the data array will make IE barf.
Another note: Use levels.sort(function(x,y){return x.localeCompare(y)}) instead of a plain .sort(). The normal sort function would sort ["abc", "Abcd", "Ab"] to ["Ab", "Abcd", "abc"]. My proposal would result ["Ab", "abc", "Abcd"].
@Jens That was a copy-pasta syntax error. Rob Thanks! I will edit my answer.
1

This should reformat the flat PHP array to the nicer JS object:

var fromPHP = ['Level 2','chapter 1','chapter 2','Level 4','chapter 1','chapter 2','Level 1','chapter 1','chapter 2','Level 3','chapter 1','chapter 2'];

var levels = [],
    betterArray = [fromPHP[0]],
    currentLevel=betterArray[0];

for (var i=1;i<fromPHP.length;i++) {
  if (fromPHP[i].substr(0,5) == 'Level') {
    currentLevel = [];
    levels.push(fromPHP[i]);
    betterArray[fromPHP[i]] = currentLevel;
  } else {
    currentLevel.push(fromPHP[i]);
  }
}

Should give the following levels and betterArray:

// levels:
['Level 4','Level 3','Level 1','Level 2']

// betterArray:
{
    'Level 2': ['chapter 1','chapter 2'],
    'Level 4': ['chapter 1','chapter 2'],
    'Level 1': ['chapter 1','chapter 2'],
    'Level 3': ['chapter 1','chapter 2']
}

Now you can run whatever sorting you want on the subarrays and get what you wanted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.