1

Possible Duplicate:
Sorting objects in an array by a field value in JavaScript

  1. What is the best way to sort "rows" by either field1, field2, or field3?

  2. What if field1, field2, or field3 is a non-numeric value (string or date), how can I make it possible to sort by these different data types?

    var Id = 0;
    var rows = new Array(); // I want to sort this array by either field1, field2, or field3
    rows[0] = (1, 3, 5);
    rows[1] = (-2, 4, -6);
    
    function Row(field1, field2, field3) {
        this.RowId = Id++;
    
        this.Field1 = field1;
        this.Field2 = field2;
        this.Field3 = field3;
    }
    
Community
  • 1
  • 1
sooprise
  • 22,657
  • 67
  • 188
  • 276
  • 1
    `(1,3,5)` is an expression that evaluate to `5`. Are you sure you don't mean `[1,3,5]`, or maybe `new Row(1, 3, 5)`? – Alex Turpin Jan 17 '12 at 16:25
  • Good call, thanks for the link, I should have done a more thorough search. – sooprise Jan 17 '12 at 16:26
  • Your code doesn't make any sense so the question can't be answered until you fix/clarify. `rows[0] = (1,3,5);` does not put multiple elements into `rows[0]`. Did you mean `rows[0] = [1,3,5];`? Or did you mean `rows[0] = {field1:1, field2:3, field3:5};`? – jfriend00 Jan 17 '12 at 16:28

0 Answers0