0

I have an array which contains 3 elements:

Field1
Field2
Field3

I need to sort this array by Field2. I have been trolling through the internet and all I seem to see is arrays with only one element being sort.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Martin Shinks
  • 115
  • 2
  • 6
  • 16
  • possible duplicate of [How to sort an array of objects with jquery or javascript](http://stackoverflow.com/questions/5503900/how-to-sort-an-array-of-objects-with-jquery-or-javascript) – Richard Dalton Mar 28 '12 at 08:15
  • Have a look at http://stackoverflow.com/questions/979256/how-to-sort-an-array-of-javascript-objects and adapt it to your needs – ori Mar 28 '12 at 08:16

2 Answers2

2

You can supply a comparison function to the sort method:

 theArray.sort(function(a, b){
   if (a.Field2 == b.Field2) return 0;
   if (a.Field2 < b.Field2) return -1;
   return 1;
 });
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

Sorry, but I don't understand your problem. To sort an need a condition to specify an order.

When you sort this array by Field2 then I guess Field2 would be the first element. And What would come second? And why? Field3 or Field1?

Thikron
  • 86
  • 1
  • 3