How to sort a list of data classes by multiple columns in JavaScript

3 Answers

0 votes
// A record-like factory function 
function Item(a, b, label) {
  return Object.freeze({ a, b, label });
}

// Create dataset
function makeData() {
  return [
    Item(7, 2, "python"),
    Item(8, 3, "c"),
    Item(3, 5, "c++"),
    Item(4, 1, "c#"),
    Item(3, 2, "java"),
    Item(7, 1, "go"),
    Item(1, 2, "rust")
  ];
}

// Sort by a, then b
function sortData(data) {
  data.sort((x, y) =>
    x.a !== y.a ? x.a - y.a : x.b - y.b
  );
}

// Print all items
function printData(data) {
  for (const item of data) {
    console.log(`(${item.a}, ${item.b}, ${item.label})`);
  }
}

// Find first item by label
function findByLabel(data, label) {
  return data.find(item => item.label === label) || null;
}

// Filter by first field
function filterByA(data, value) {
  return data.filter(item => item.a === value);
}

// Main program
function main() {
  const data = makeData();

  sortData(data);
  console.log("Sorted data:");
  printData(data);

  console.log("\nSearching for 'java':");
  const found = findByLabel(data, "java");
  if (found) console.log(found);

  console.log("\nFiltering items where a == 7:");
  const filtered = filterByA(data, 7);
  printData(filtered);
}

main();



/*
run:

Sorted data:
(1, 2, rust)
(3, 2, java)
(3, 5, c++)
(4, 1, c#)
(7, 1, go)
(7, 2, python)
(8, 3, c)

Searching for 'java':
{ a: 3, b: 2, label: 'java' }

Filtering items where a == 7:
(7, 1, go)
(7, 2, python)

*/

 



answered Jan 29 by avibootz
0 votes
// Array of struct-like objects
const data = [
  { a: 7, b: 2, label: "python" },
  { a: 8, b: 3, label: "c" },
  { a: 3, b: 5, label: "c++" },
  { a: 4, b: 1, label: "c#" },
  { a: 3, b: 2, label: "java" },
  { a: 7, b: 1, label: "go" },
  { a: 1, b: 2, label: "rust" }
];

// Sort by a, then b
function sortData(arr) {
  arr.sort((x, y) =>
    x.a !== y.a ? x.a - y.a : x.b - y.b
  );
}

// Print all items
function printData(arr) {
  for (const item of arr) {
    console.log(`(${item.a}, ${item.b}, ${item.label})`);
  }
}

// Find first item by label
function findByLabel(arr, label) {
  return arr.find(item => item.label === label) || null;
}

// Filter by a
function filterByA(arr, value) {
  return arr.filter(item => item.a === value);
}

// Main program
function main() {
  const items = [...data]; // copy so original stays unchanged

  sortData(items);
  console.log("Sorted data:");
  printData(items);

  console.log("\nSearching for 'java':");
  const found = findByLabel(items, "java");
  if (found) console.log(found);

  console.log("\nFiltering items where a == 7:");
  const filtered = filterByA(items, 7);
  printData(filtered);
}

main();



/*
run:

Sorted data:
(1, 2, rust)
(3, 2, java)
(3, 5, c++)
(4, 1, c#)
(7, 1, go)
(7, 2, python)
(8, 3, c)

Searching for 'java':
{ a: 3, b: 2, label: 'java' }

Filtering items where a == 7:
(7, 1, go)
(7, 2, python)

*/

 



answered Jan 29 by avibootz
edited Jan 29 by avibootz
0 votes
// Data class
class Item {
  constructor(a, b, label) {
    this.a = a;
    this.b = b;
    this.label = label;
  }

  toString() {
    return `(${this.a}, ${this.b}, ${this.label})`;
  }
}

// Your data
const data = [
  new Item(7, 2, "python"),
  new Item(8, 3, "c"),
  new Item(3, 5, "c++"),
  new Item(4, 1, "c#"),
  new Item(3, 2, "java"),
  new Item(7, 1, "go"),
  new Item(1, 2, "rust")
];

// Sort by a, then b
function sortData(arr) {
  arr.sort((x, y) =>
    x.a !== y.a ? x.a - y.a : x.b - y.b
  );
}

// Print all items
function printData(arr) {
  for (const item of arr) {
    console.log(item.toString());
  }
}

// Find first item by label
function findByLabel(arr, label) {
  return arr.find(item => item.label === label) || null;
}

// Filter by a
function filterByA(arr, value) {
  return arr.filter(item => item.a === value);
}

// Main program
function main() {
  const items = [...data]; // copy so original stays unchanged

  sortData(items);
  console.log("Sorted data:");
  printData(items);

  console.log("\nSearching for 'java':");
  const found = findByLabel(items, "java");
  if (found) console.log(found.toString());

  console.log("\nFiltering items where a == 7:");
  const filtered = filterByA(items, 7);
  printData(filtered);
}

main();




/*
run:

Sorted data:
(1, 2, rust)
(3, 2, java)
(3, 5, c++)
(4, 1, c#)
(7, 1, go)
(7, 2, python)
(8, 3, c)

Searching for 'java':
(3, 2, java)

Filtering items where a == 7:
(7, 1, go)
(7, 2, python)


*/

 



answered Jan 29 by avibootz

Related questions

...