2

I have an array with the following structure:

[[],[{"id":1,"meaning":1,"word":"b"},{"id":2,"meaning":1,"word":"a"}],[{"id":3,"meaning":2,"word":"f"},{"id":4,"meaning":2,"word":"c"}],[{"id":5,"meaning":3,"word":"d"}]]

array[0] needs to be empty, because I need that index for a special usage later on. array[1] for example contains all objects with meaning:1, array[2] all those with meaning:2.

What I want to do now is to sort this array by the first object in the 2D-Array(so to say by the first column).

Thus the output should be like:

[[],[{"id":1,"meaning":1,"word":"b"},{"id":2,"meaning":1,"word":"a"}], [{"id":5,"meaning":3,"word":"d"}], [{"id":3,"meaning":2,"word":"f"},{"id":4,"meaning":2,"word":"c"}]]

I would appreciate all types of answers.

EDIT: The sort criterion is an ascending alphabetic order

3
  • By the way, I tried to use the standard .sort() method, but it didn't do what I expected. I take use of AngularJS and JQuery in my application. Commented May 1, 2015 at 11:53
  • 1
    I'm not sure I understand what you're asking. You have an array of arrays of objects, but what is your sort criteria? The lowest id in the collection with the same value for meaning? The lowest value of meaning? btw, sort takes a sorting function as an argument... Commented May 1, 2015 at 11:58
  • I have edited my question, the sort cirterion is an ascending alphabetic order. Commented May 1, 2015 at 12:06

4 Answers 4

0

First off, ditch the empty array, as its much easier to add that later than to code around it in your sorting function. Assuming your top level array is called 'arr':

//first we'll sort the sub arrays themselves
var newArr = arr.map(function(subArray) {
        subArray.sort(function(a, b) {
            if (a.word > b.word) {
                return -1;
            } else {
                return 1;
            }
        });
        return subArray;
    })

    //and now sort the containing array
    .sort(function(a, b) {
        if (a[0].word > b[0].word) {
            return -1;
        } else {
            return 1;
        }
    });

//now we'll add back in your empty array
newArr = [[]].concat(newArr);

if the sub arrays are already in alphebetical order than you only need the global sort.

1
  • i had to swap the returns of the sort function, but nevertheless, your answer was the simplest and did what i wanted to, thank you Commented May 2, 2015 at 11:10
0

Did you know that sort may take a function as an argument? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

1
  • Yes I did read of that. But I don't know what to do to only access the first object of the first column (array[i][0]) in this function Commented May 1, 2015 at 12:07
0

Sorting is a comparison, if I understand you correctly you just need to rearrange the objects based on the meaning property. This should do the trick for you, of course the sorting on word is up to you.

function arrangeByMeaning (arr) {
    arr.forEach(function (innerArr, index) {
        innerArr.forEach(function (obj, i) {
            if (parseInt(obj.meaning) !== index) {
                var o = innerArr.pop(i);
                arr[index].push(o);
            }
        });
    });

    // sort innerArrays on word after rearranging objects
    arr.forEach(function (innerArr) {
        innerArr.sort(function (a, b) { 
            return a.word < b.word ? -1 : a.word > b.word ? 1 : 0;
        });
    });

    return arr;
};

This will modify the original array.

Fiddle

1
  • thanks for the answer, but the order of the object in an array souldn't be changed, only the order of the arrays Commented May 1, 2015 at 12:57
0

Here you go. Bit untidy but works: http://jsfiddle.net/8xv6s9g1/2/

var array = [[],[{"id":1,"meaning":1,"word":"b"},{"id":2,"meaning":1,"word":"a"}],[{"id":3,"meaning":2,"word":"f"},{"id":4,"meaning":2,"word":"c"}],[{"id":5,"meaning":3,"word":"d"}]];

var arrayCopy = array.slice();
for(var i = 0; i < arrayCopy.length; i++) {
    arrayCopy[i].sort(function(x, y) {
        if(!x || x.word < y.word) return 1;
        if(x.word > y.word) return -1;
        return 0;
    });
}
arrayCopy.sort(function(x, y) {
        if(!x.length && !y.length) return 0;
        if(!x.length) return -1;
        if(!y.length) return 1;

        if(x[0].word < y[0].word) return -1;
        if(x[0].word > y[0].word) return 1;
        return 0;
    });

for(var i = 0; i< arrayCopy.length; i++) {
    arrayCopy[i].index = i;
}

array.sort(function(x, y) {
        if(!x || x.index < y.index) return -1;
        if(x.index > y.index) return 1;
        return 0;
    });

console.log(array);

Output is [[],[{"id":1,"meaning":1,"word":"b"},{"id":2,"meaning":1,"word":"a"}], [{"id":5,"meaning":3,"word":"d"}], [{"id":3,"meaning":2,"word":"f"},{"id":4,"meaning":2,"word":"c"}]] as required

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.