-3

So I grab a bunch of information from a few databases to get an array of values, their names and order being:

['type', 'baseid', 'lotid', 'split', 'sub', 'sequence', 'resourceid', 'user1', 'user2', 'part', 'department']

So I have an array of those, which I put into a tableview and I need to resort the array by the department value without changing any of the order of the internal array, so that in the table all assemblies are together, all GMBEAD BLASTs are together ect...

An example of the array with values looks like:

[[EDM, M, 887013393, C, 0, 2, 50, GE_1_INS], [QUALITY, M, 887013393, C, 0, 2, 60, GMDEBURR], [FINISH, M, 887013393, C, 0, 2, 70, GMBEAD BLAST]]

Yes I have tried a few .sort or .sort_by methods but with the numbers in the array it wont let me even run the program when I put in a sorting method.

AGS
  • 14,288
  • 5
  • 52
  • 67

2 Answers2

1

What do you mean by "it wont let me even run the program"? Assuming the department is always the last column, a simple my_array.sort{ |a,b| a.last <=> b.last } should do the trick.

christianblais
  • 2,448
  • 17
  • 14
1

You want something like this:

sorted_array = your_array.sort_by {|inner_array| inner_array.last }

The errors you're getting make it sound like you're trying to sort the inner arrays, which won't work.

Wally Altman
  • 3,535
  • 3
  • 25
  • 33
  • Thanks man this one worked excelent! Ya I was runing into a conversion of fixnum errors when I tried to do a similare version of this, which made no since to me...But as usual now that my programing error is fixed I feel like a morron. – Max Iglehart Apr 08 '13 at 14:52