User37
User37

Reputation: 31

Sorting csv according to particular column

Hi I am exporting data to csv file using javascript. I need to sort the data according to specific column index.

Code:

      $.each(exportArray, function (index, value) {
csvData.push(x[index] + "," + y[index] + "," + d[index] + "," + z[index]  + ","+ a[index] + "," + e[index] + "," + b[index] + "," + c[index]);
        });
csvData.sort();

         csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(output);

      $(this)
.attr({
    'download': filename,
    'href': csvData,
    'target': '_blank'
});

E.g.: csvData.sort() is sorting data acoording to first column as default. I want to have it sorted according to the third column i.e.. by d[index]. I tried this csvData.sort(sort_by(d[index])); and it is not working.

I have one more issue . Since I am exporting the data to csv. Now in d[index] from the server if I have three values like d[index]="cat,dog,bat" . It is getting displayed in 3 adjacent columns. I want it in the same column. Can I change the delimiter to something else from comma.

Code:

       csvData.push(x[index] + "," + y[index] + "," + d[index] + "," + z[index]  + ","+ a[index] + "," + e[index] + "," + b[index] + "," + c[index]);

Upvotes: 1

Views: 7845

Answers (2)

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26946

You have to write a custom compare function

// The following function is used to sort on the basis of the third element
function compareByThird(a, b) {
    var aArr = a.split(',');
    var bArr = b.split(',');
    return aArr[2] - bArr[2];
}

csvData.sort(compareByThird);

It works if the third column is a numeric value. If not (for example for strings) it necessary to use the following code:

// The following function is used to sort on the basis of the third element
function compareByThird(a, b) {
    var aArr = a.split(',');
    var bArr = b.split(',');
    if (aArr[2] < bArr[2]) {
        return -1;
    } else if (aArr[2] > bArr[2] {
        return 1;
    } else {
        return 0;
    }
}

Upvotes: 0

Anders
Anders

Reputation: 8588

You need to sort the data before you write it to the CSV. This is more easily done if the data is in an array of objects instead of spread out in one array per property. To achive this you can either just put the data in objects when it is created or read, or you can convert it with a snippet like this:

var count = x.length;
var objects = new Array(count);
for(i=0; i++; i<count) {
    objects[i] = {
        x: x[i],
        y: y[i],
        d: d[i],
        z: z[i],
        a: a[i],
        e: e[i],
        b: b[i],
        c: c[i],
    };
}

Then you can easily sort the data, for example by the x property, with this code:

function compare(a, b) {
  if (a.x < b.x) return -1;
  if (a.x > b.x) return 1;
  return 0;
}

objects.sort(compare);

Then you will get the arrays sorted, and can access individual properties with for instance objects[i].x.

Upvotes: 1

Related Questions