0

I'm new to angular2 and I'm trying to sort out my food order list by collection time datestamp coming from the database.

let say the order object is below and in my template I want to sort it with earliest collectiontime in the example below it would be id: 2454

var awaitingPicking = [
  {
   "id": "2452",
   "OrderLineGroups": [
     {
       "CollectionDetails": {
         "CollectionFrom": "2017-03-21T11:00:00.317"
       }
     }
   ]
  },
{
   "id": "2454",
   "OrderLineGroups": [
     {
       "CollectionDetails": {
         "CollectionFrom": "2017-03-21T11:00:00.317"
       }
     }
   ]
  }
 ]

Image shows how my list is being rendered in my HTML and it puts id: 2454 at the bottom when it should be before id: 2452.

Edit --

this.awaitingPicking.push(element); // these holds all order objects
this.awaitingPicking.map(e => {
  this.getCollectionFrom = e.CollectionFrom = e.OrderLineGroups[0].CollectionDetails.CollectionFrom
   return e
})

Template --

<div *ngFor="let prepare of awaitingPicking | orderBy: '+getCollectionFrom'" id="prepareOrder"> </div>

How can I sort the list with earliest collectionFrom?

sample image

aukey
  • 13
  • 4
  • 2
    Possible duplicate of [angular 2 sort and filter](http://stackoverflow.com/questions/32882013/angular-2-sort-and-filter) – silentw Mar 21 '17 at 12:37
  • I've updated my question and tried this plnkr from the link you include https://plnkr.co/edit/DU6pxr?p=preview but it didn't sort out by collectionFrom – aukey Mar 21 '17 at 12:53
  • I don't see collectionFrom anywhere in your plunkr, and it is sorting by the date field you supplied. – roberto tomás Mar 21 '17 at 12:59
  • the `var` is only an example but it would have the same value anyways, but the real object porperty is "CollectionFrom" but that shouldn't be the case. – aukey Mar 21 '17 at 13:04
  • @robertotomás I've edited my sample `var` object to map out the actual `CollectionFrom` property – aukey Mar 21 '17 at 13:10
  • @kram please update your plunkr – roberto tomás Mar 21 '17 at 13:12
  • this https://plnkr.co/edit/DHLVc0?p=preview works but its sorting it my date, need to sort it by time like (11:00am) – aukey Mar 21 '17 at 13:22
  • @kram wow, that is ugly. stop making such a mess and just edit the plunkr you had before, to contain a minimum expample. The second one has a ton of other crap you dont care about but still not the data that you actually want to use. Focus! :) – roberto tomás Mar 21 '17 at 13:28
  • @robertotomás those aren't my plnkr, that's from the link SilentW provided as he marked it as duplicate. Read my comment after his! – aukey Mar 21 '17 at 13:40
  • @kram - well, you said it *almost* works for your data, and we shouldn't have to guess what the code looks like you should provide it. So move a sample of your data in place of the sample data in the first [plunkr](http://plnkr.co/edit/DU6pxr?p=preview), and get it as close to working as you can, and then save the plunkr as your own and post the new link here. Once you have a plunkr of your problem I will look at it. – roberto tomás Mar 21 '17 at 13:49
  • @robertotomás looks like its not working for my case, the sample was using `new Date()` where as mine is a date `string` https://plnkr.co/edit/TYXh2iCDseJNnT0DwKA7?p=preview – aukey Mar 21 '17 at 14:09

1 Answers1

0

the solution uses a custom filter that only goes one level deep, so your data must also be only 1 level deep:

https://plnkr.co/edit/rCGc2IZECuPiL3QqNnlJ?p=preview (updated)

changes:

app.ts

//...
@Component({
  selector: 'my-app',
  providers: [],
  pipes: [DatePipe, ArraySortPipe],
  template: `
    <ul>
      <li *ngFor="#item of sortableArray | arraySort:'+CollectionFrom'">{{item.id}} {{item.CollectionFrom}}</li>
    </ul>
  `,
  directives: []
})
//...
export class App {

  inputArray: Array<any> = [
      {
     "id": "2452",
     "OrderLineGroups": [
       {
         "CollectionDetails": {
           "CollectionFrom": "2017-03-21T11:40:00.317"
         }
       }
     ]
    },
  {
     "id": "2454",
     "OrderLineGroups": [
       {
         "CollectionDetails": {
           "CollectionFrom": "2017-03-21T11:00:00.317"
         }
       }
     ]
    }
  ]

  sortableArray: Array<any> = this.inputArray.map(e => {
    e.CollectionFrom = e.OrderLineGroups[0].CollectionDetails.CollectionFrom
    return e
  })

  constructor() {}
}

//...
roberto tomás
  • 4,435
  • 5
  • 42
  • 71
  • Your change didn't make `id: 2454` to be the first in the list. Early collection time should be first `2017-03-21T11:00:00.317` – aukey Mar 21 '17 at 14:38
  • @kram I don't think you understood the solution they wrote! I blanked too, my first instinct was to just remove the sort modifier :) I've changed the plunkr and code above to give the output you want. You have to change the template from a -CollectionFrom (descending) to a +CollectionFrom (ascending) sort order: `
  • {{item.id}} {{item.CollectionFrom}}
  • `. the answer is updated – roberto tomás Mar 21 '17 at 15:30
  • can you check my edit in my question, isn't that similar to yours? if so, somehow mine its not sorting out. – aukey Mar 21 '17 at 16:33
  • @kram please show the console.log of awaitpicking after the map. This part `this.getCollectionFrom = e.CollectionFrom = e` is redundant but seems fine – roberto tomás Mar 21 '17 at 22:17
  • why did you do `sortableArray: Array = this.inputArray`? Can you explain that bit please. – aukey Mar 22 '17 at 20:53
  • @kram sure. I assumed you might not want to alter your original array, so I made a copy and altered it (the full statement is `this.inputArray.map(...`) – roberto tomás Mar 22 '17 at 21:29