0

Following the answer from David Lewis in Javascript - sort array based on another array, I am trying to sort table data, based on the order of a second nested array of a property called clientRef.

vm.tableData = transactions; //populated with data from response stored in variable (original data)

var clientRefArr = [];

vm.tableData.forEach(function(data) {
    var res = data.renewalUIs.map(function(o){ 
        clientRefArr.push(o.patentUI.clientRef) //The data in which I need to sort the table with
    })
})

clientRefArr.sort(); //sort array

var aSorted = [], bSorted = []; //need arrays of the clientRef

function sortFunc(a, b) {

    a.renewalUIs.forEach(function(data, i, index){
        aSorted.push(data.patentUI.clientRef);
    })

    b.renewalUIs.forEach(function(data, i){
        bSorted.push(data.patentUI.clientRef);
    })

    return clientRefArr.indexOf(aSorted[1]) - clientRefArr.indexOf(bSorted[1]); //following the answer from the linked stackoverflow, I compare the two arrays

}

vm.tableData.sort(sortFunc)

I need to sort the outter array of vm.tableData based on the order of clientRef array, some of which have a null value. I have been unsuccessful so far and not sure how to approach this.

Question

How do I sort vm.tableData, based on the order of the clientRef array?

**clientRef array** (order array)

[
 0:""
 1:""
 2:"CWORD"
 3:"DWORD"
 4:"FWORD"
 5:"GWORD"
 6:"HWORD"
]

**vm.tableData** (data to be sorted)

[
 {
   status: 'awaiting funds'
   transRef: 'TRAN000192'
   renewalUIs: [
    {
     patent: null
     fee: 1500
     patentUI: {
      patentNo: EP27237
      bandColour: green
      clientRef: FWORD //here is the nested clientRef which I then sort in the clientRefArr
     }
    }
   ]
 }
]
Patrick McDermott
  • 1,220
  • 1
  • 15
  • 30
  • 1
    What's in the arrays? What does "based on the alphabetical order" mean even? It would help a lot if you provided sample array contents and sort input/output. – Pointy Mar 14 '18 at 12:58
  • What does `vm.tableData` look like ? And `clientRefArr` ? – Isac Mar 14 '18 at 12:59
  • Apologies guys. I rushed the question. I have provided the clientRef array and the structure of `vm.tableData` – Patrick McDermott Mar 15 '18 at 09:18

0 Answers0