tags:

views:

52

answers:

2

Hello! I have a js object (name, noWords, score, group)

flower - 1 - 88 - 0
flower - 1 - 99 - 0
flower,spring - 2 - 39 - 1
flower,spring - 2 - 58 - 1
flower,time - 2 - 20 - 2
flower,time - 2 - 53 - 2
spring,time - 2 - 55 - 3
flower,spring,time - 3 - 79 - 4
flower,spring,time - 3 - 121 - 4

I want to sort this object like this first matter the number of words - it's made then if there are more groups with the same number of words to sort by greater value of each group

Expected result

flower - 1 - 88 - 0
flower - 1 - 99 - 0
flower,time - 2 - 20 - 2
flower,time - 2 - 53 - 2
spring,time - 2 - 55 - 3
flower,spring - 2 - 39 - 1
flower,spring - 2 - 58 - 1
flower,spring,time - 3 - 79 - 4
flower,spring,time - 3 - 121 - 4
A: 

First of all you mean Array. You can't sort an Object.

What you need is to perform an user sort. To do this you use the Array.sort() method along with a custom sorting function. In that function you put you comparison algorithm.

Simple example:

myArray = [5, 3, 7, 9];
myArray.sort(function(a, b){
    if(a > b)
        return -1; // negative means that the elements are not in the right order and should be switched
    else
        return 1; // positive means that the element are in the desired order
});

The sort method does all the sorting for you but you have to provide the means of comparing any two elements.

Alin Purcaru
Yes, i know this approach ... but i want something more complicated. I have 3 groups of 2 words. I want to sort the groups by the higher element of each group. Like in expected result you will see flower,time - 2 - 20 - 2flower,time - 2 - 53 - 2spring,time - 2 - 55 - 3flower,spring - 2 - 39 - 1flower,spring - 2 - 58 - 1
tinti
Maybe you should give more details in your question.
Alin Purcaru
+1  A: 

assuming your objects are in an array, you can use a custom sort method for comparing two entries.

var myObjects = [ /* assuming this is filled. */ ];

myObjects.sort(function (a, b) {

    // a and b will be two instances of your object from your list

    // possible return values
    var a1st = -1; // negative value means left item should appear first
    var b1st =  1; // positive value means right item should appear first
    var equal = 0; // zero means objects are equal

    // compare your object's property values and determine their order
    if (b.noWords < a.noWords) {
        return b1st;
    }
    else if (a.noWords < b.noWords) {
        return a1st;
    }

    // noWords must be equal on each object
    if (b.group < a.group) {
        return b1st;
    }
    else if (a.group < b.group) {
        return a1st;
    }

    // group must be equal
    // TODO continue checking until you make a decision

    // no difference between objects
    return equal;
});

Your description of how you want things ordered is unclear so I'll leave that part up to you.

lincolnk
So, last values of my example are represents the group id
tinti
@tinti sorry, between your descriptions and your expected result I still don't know how to order your comparisons. you should be able to follow the general flow of comparisons in the example to finish it out for your specific case.
lincolnk
So, last values of my example are represents the group id.I want to find maximum (value at position 2, 0 based)from each group and sort the groups with same number of elements (split for ',' of values at pos 0) by the maximum of each group.See my example.I must have kind of 3 sorts.First sort by the number of elements split by , the second sort is the group with same number elements and sorted by highest values (pos 2 in my line) and last sort is the sort of each group (pos3 in my line) by 3rd value.example (flower,spring,time-3-121- 4) means (pos0-pos1-pos2-pos3) I hope now i'm clear.Thanks!!
tinti
Sorry, i edit my answer but you reply so fast so i didn't have time to complete all the comment. Now i made it. Thanks in advance
tinti