I need help grouping an Array in my JavaScript code

mainrotor
mainrotor used Ask the Experts™
on
Hi Experts,
I am working on a MVC.Net application with C#.Net code-behind.

I am using an Array in one of my views.
I am able to populate my array just fine, but I need to do the following once I have populated my array:
1. Group by the following fields: order, item, description
2. Sort by the following fields: order, item, description

I have included my javascript code below.  
Please show me how to do my grouping and sorting of my javascript array.

var claimsHeader = new Array(); 



function onDataBoundHeader(arg) {
	var dataItemsHeader = arg.sender.dataSource.view();

        for (var j = 0; j < dataItemsHeader.length; j++) {
            var order = dataItemsHeader[j].get("Item");
            var item = dataItemsHeader[j].get("Market");
            var description = dataItemsHeader[j].get("Description");
            
            var claims = new Array(order, item, description);
            claimsHeader.push(claims);
        }
}

Open in new window


Thank you in advance,
mrotor
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
When you say " Group by the following fields: order, item, description" how do you mean group - can you explain a bit more.

For sorting just use the Array.sort method.

claimsHeader.sort(function(a, b) {
   if (a.order < b.order) return -1;
   if (a.order > b.order) return 1;
   if (a.item < b.item) return -1;
   if (a.item > b.item) return 1;
   if (a.description < b.description) return -1; // You may need to use strtolower if you don't want case to be a factor
   if (a.description > b.description) return 1;
   return 0;
});

Open in new window

Author

Commented:
The array I create, is generated from a grid.  The data that loads into my array is sometimes repeated.
For example:

ORDER     ITEM          DESCRIPTION
ABC          SHIRT         SILK SHIRT
ABC          SHIRT         SILK SHIRT
DEF           PANTS       POLYESTER PANTS

I want the array to group by ORDER, ITEM, and DESCRIPTION so that the results are grouped as follows:
ABC          SHIRT         SILK SHIRT
DEF           PANTS       POLYESTER PANTS



How can I do this?
NorieAnalyst Assistant

Commented:
Do you mean you want to an array unique 'records' and then sort?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Yes, Norie.  I want an array's unique records and then sort.

Thank you for helping me clarify what it is I need.  :-)

How can I accomplish this?
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
// set data to your incoming data, claimsHeader will be our output array
<script>
var data = [{
  order: 'ZZZ',
  item: 'PANTS',
  description: 'POLYESTER PANTS'
},{
  order: 'ABC',
  item: 'ZANTS',
  description: 'POLYESTER PANTS'
},{
  order: 'ABC',
  item: 'SHIRT',
  description: 'SILK SHIRT'
},{
  order: 'ABC',
  item: 'SHIRT',
  description: 'SILK SHIRT'
},{
  order: 'DEF',
  item: 'PANTS',
  description: 'POLYESTER PANTS'
}];
var claimsHeader = [];
// First filter the data
data.forEach(function(item) {
   var found = claimsHeader.find(function(a) {
       return a.order == item.order && a.item == item.item && a.description == item.description;
   });
   if (!found) claimsHeader.push(item);
});
// Now sort the data
claimsHeader.sort(function(a, b) {
   if (a.order < b.order) return -1;
   if (a.order > b.order) return 1;
   if (a.item < b.item) return -1;
   if (a.item > b.item) return 1;
   if (a.description < b.description) return -1; // You may need to use strtolower if you don't want case to be a factor
   if (a.description > b.description) return 1;
   return 0;
});
console.log(claimsHeader);
</script>

Open in new window

Author

Commented:
Thanks Julian,
I will try your suggestion and report my results.

mrotor

Author

Commented:
Thanks Julian.
Your suggestion worked.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial