0

I am working on a project in my javascript class and I am having trouble sorting parallel arrays. I have successfully sorted them alphabetically, however I also need to sort in ascending order. I have tried using a key to sort, but the professor said we are not allowed to alter the arrays given.I need to be able to sort the mnthCharge but keep the names and other data in line when they are sorted. Any pointers would be greatly appreciated!

var custName = new Array("Smith, Al","Toms, Andy","Jones, Zack","Vargas, Eddie","Donner, Alice","McMullen, Jessie","Nevins, Carol","Stark, Howard","Neeland, Franny","Boxby, Amos");

  var mnthCharge = new Array(140.00, 42.00, 18.00, 18.00, 36.00, 140.00, 42.00, 24.00, 140.00, 24.00);

var pastDueAmt = new Array(0, 0, 84.00, 36.00, 0, 18.00, 42.00, 42.00, 96.00, 0);

  var dayPastDue = new Array(0, 0, 60, 60, 0, 30, 90, 30, 120, 0);
  
  function recurringCharges()
{
  var d = [];
for(var i=0; i<custName.length; i++){
  //d.push(['Customer': custName[i], 'Recurring': mnthCharge[i], 'Amount': pastDueAmt[i], 'Days': dayPastDue[i]]);
    d.push([custName[i], mnthCharge[i],pastDueAmt[i],dayPastDue[i]]);
}
var sorted = d.sort();

2 Answers2

1

Here is a method that requires no change to the original data structures.

Suppose you have two parallel arrays v and w, of equal lengths.

First, generate an array with numeric values 0...(v.length)-1; let us call it ix:

var ix = v.map((x,i) => i);

Then sort ix, instead of either v or w. Of course, the compare function must be designed with the fact it is sorting indexes in mind; for example (supposing v contains numeric values):

ix.sort((a,b)=>v[a] - v[b]);

Now ix gives you an array of indexes you can use to access both v and w, in sorted order.

One thing you can do is rearrange v and w so they are actually sorted:

v = ix.map(x=>v[x]);
w = ix.map(x=>w[x]);

But you need not do that, you can instead use indirection through ix; for example:

for (let i = 0; i < ix.length; i++)
    alert(v[ix[i]] + " " + w[ix[i]]);

Note 1: This is how sorting has always worked in APL: sort always returns an array of indexes.

Note 2: A conceptually simpler way to generate ix is:

var ix = [];
for (let i = 0; i < v.length; i++)
    ix.push(i);

Note 3: The sort criterion can involve both v and w; for example, you could sort in increasing order of v[i]+w[i]:

ix.sort((a,b)=>(v[a]+w[a]-v[b]-w[b]));
0

Specify the item to sort by, and use {} instead [] for object notation:

var custName = ["Smith, Al","Toms, Andy","Jones, Zack","Vargas, Eddie","Donner, Alice","McMullen, Jessie","Nevins, Carol","Stark, Howard","Neeland, Franny","Boxby, Amos"]
var mnthCharge = [140.00, 42.00, 18.00, 18.00, 36.00, 140.00, 42.00, 24.00, 140.00, 24.00]
var pastDueAmt = [0, 0, 84.00, 36.00, 0, 18.00, 42.00, 42.00, 96.00, 0]
var dayPastDue = [0, 0, 60, 60, 0, 30, 90, 30, 120, 0]

for(var d = [], i = 0; i < custName.length; ++i) 
    d.push({ Customer: custName[i], Recurring: mnthCharge[i], 
             Amount: pastDueAmt[i], Days: dayPastDue[i]       })   // {} instead of []

d.sort(function(a, b) { return (a = a.Customer) > (b = b.Customer) || -(a < b) })

console.log( JSON.stringify( d ).replace(/},/g, '},\n ') )
Slai
  • 22,144
  • 5
  • 45
  • 53
  • Don't use `localeCompare` when you don't need a special locale – Bergi Dec 06 '17 at 13:40
  • @Bergi what would you use instead .. two `<` and `>` comparisons? `d.sort(function(a, b) { return a.Customer > b.Customer })` seems to be enough in Chrome, but not sure if it's correct. – Slai Dec 06 '17 at 14:20
  • 1
    Yes, you'd [need two comparisons](https://stackoverflow.com/q/24080785/1048572): `d.sort((a, b) => +(a.customer > b.customer) || -(a.customer < b.customer))` – Bergi Dec 06 '17 at 14:25
  • I have inserted the snippet of the code and understand that it should be returning true false based on which value is greater. Is it possible to just print the values instead of returning, all while keeping the other elements in the array in the same order following the sort? –  Dec 06 '17 at 18:45
  • @ZackBunch the function passed to the sort method should technically return negative, positive, or 0 number (less than, greater than, or equal). The source arrays are not modified, so I am not sure what you mean .. maybe give an example? – Slai Dec 06 '17 at 18:54
  • I figured it out, I missed the part where you said to use brackets instead of parenthesis in the comments. However, one more question. When I go to document.write into my page, it displays [object Object],[object Object]..etc. I am lost on that part. –  Dec 06 '17 at 19:05
  • @ZackBunch you can use `document.write( JSON.stringify( d ) )` – Slai Dec 06 '17 at 19:07