I need help grouping an Array in my JavaScript code

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
mainrotorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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

0
mainrotorAuthor 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?
0
NorieAnalyst Assistant Commented:
Do you mean you want to an array unique 'records' and then sort?
0
Determine the Perfect Price for Your IT Services

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

mainrotorAuthor 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?
0
Julian HansenCommented:
// 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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mainrotorAuthor Commented:
Thanks Julian,
I will try your suggestion and report my results.

mrotor
0
mainrotorAuthor Commented:
Thanks Julian.
Your suggestion worked.
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET MVC

From novice to tech pro — start learning today.