0 votes
1.7k views
in Javascript API by

Sort an Array of Objects in JavaScript

Now let’s look at sorting an array of objects. Let’s take an array of band objects:

const bands = [
  { genre: 'Rap', band: 'Migos', albums: 2},
  { genre: 'Pop', band: 'Coldplay', albums: 4},
  { genre: 'Rock', band: 'Breaking Benjamins', albums: 1}
];

We can use the following compare function to sort this array of objects according to genre:

function compare(a, b) {
  // Use toUpperCase() to ignore character casing
  const genreA = a.genre.toUpperCase();
  const genreB = b.genre.toUpperCase();

  let comparison = 0;
  if (genreA > genreB) {
    comparison = 1;
  } else if (genreA < genreB) {
    comparison = -1;
  }
  return comparison;
}

bands.sort(compare);

/* returns [
{ genre: 'Pop', band: 'Coldplay', albums: 4 },
{ genre: 'Rap', band: 'Migos', albums: 2 },
{ genre: 'Rock', band: 'Breaking Benjamins', albums: 1 }
] */

1 Answer

0 votes
by (30.6k points)

Business object lists in BPM are TWList objects, not JavaScript arrays: tw.local.orders.sort(...) does not exist on 8.5.x (recent BAW releases expose toArray() on lists). The portable way: copy to an array, sort, copy back:

// server-side script: sort tw.local.orders (list of Order) by total, descending, then by number
var arr = [];
for (var i = 0; i < tw.local.orders.listLength; i++) arr.push(tw.local.orders[i]);
arr.sort(function (a, b) {
  if (b.total !== a.total) return b.total - a.total;                       // numbers
  return (a.number || "").localeCompare(b.number || "");                   // strings
});
var sorted = new tw.object.listOf.Order();
for (var j = 0; j < arr.length; j++) sorted.insertIntoList(j, arr[j]);
tw.local.orders = sorted;

// generic helper: sortListBy(list, field, direction)
function sortListBy(list, field, desc) {
  var arr = []; for (var i = 0; i < list.listLength; i++) arr.push(list[i]);
  arr.sort(function (a, b) {
    var x = a[field], y = b[field];
    if (x instanceof Date || (x && x.getTime)) { x = x.getTime(); y = y.getTime(); }               // dates
    var r = (typeof x === "number" && typeof y === "number") ? x - y : String(x == null ? "" : x).localeCompare(String(y == null ? "" : y));
    return desc ? -r : r;
  });
  return arr;                       // caller copies back into a typed list (see below)
}
// simpler generic helper: sort in place by replacing the elements (keeps the list type)
function sortList(list, cmp) {
  var arr = []; for (var i = 0; i < list.listLength; i++) arr.push(list[i]);
  arr.sort(cmp);
  for (var j = 0; j < arr.length; j++) list[j] = arr[j];
}
sortList(tw.local.orders, function (a, b) { return new Date(b.created) - new Date(a.created); });   // newest first

Client side (coach): the bound list of a UI Toolkit table is a plain array with items on some releases - use var d = ${Table}.getData(); d.sort(cmp); ${Table}.setData(d);, or simply make the columns sortable and let the user click. Stable sorting: JavaScript's sort is stable in modern engines and in Rhino on BAW; on old Rhino add the index as a tiebreaker.

References

Related questions

0 votes
1 answer 2.3k views
0 votes
1 answer 942 views
0 votes
1 answer 761 views
0 votes
1 answer 2.0k views
0 votes
1 answer 2.1k views
+1 vote
1 answer 2.5k views
0 votes
2 answers 2.9k views
+1 vote
1 answer 1.8k views
0 votes
1 answer 3.6k views
+1 vote
1 answer 1.2k views
0 votes
1 answer 849 views
0 votes
1 answer 1.3k views

723 questions

807 answers

98 comments

4.9k users

Join BPM Community Discord Channel

Welcome to BPM Tips Q&A, Community wiki/forum where you can ask questions and receive answers from other IBM BPM experts and members of the community. Users with 2000 points will automatically be promoted to expert level.
Created by Dosvak LLC
Our Youtube Channel
...