VadimDez / ngx-order-pipe

▼ Angular 5+ orderBy pipe
https://vadimdez.github.io/ngx-order-pipe/
MIT License
243 stars 57 forks source link

Sorting the column if they have same values - error #72

Closed SrideviYerneni closed 5 years ago

SrideviYerneni commented 6 years ago

Hi. When I apply sorting to the table, if the column has all same values, and when I click on the column to sort, nothing should happen. But it is sorting other columns when I'm clicking on the columns with same values. Here's the collection :

"collection": [
            {
                "name": "John",
                "age" : "25",
                "score": 12,
                "percent": 5.2%

            }
            {
                "name": "Mark",
                "age" : "19",
                "score": 12,
                "percent": No Value
            }
            {
                "name": "Peter",
                "age" : "21",
                "score": 12, 
                "percent": 1.8%

html:

 <table>
      <thead>
        <tr>
          <th (click)="setOrder('name')">Name</th>
          <th (click)="setOrder('age')">Age</th>
          <th (click)="setOrder('score')">Score</th>
          <th (click)="setOrder('percent')">Percent</th>
        </tr>
      </thead>
      <tbody>
        <tr *ngFor="let Data of collection | orderBy: order:reverse:'case-insensitive'">
          <td class="text-truncate">{{Data.name}}</td>
          <td class="text-truncate">{{Data.age}}</td>
          <td class="text-truncate">{{Data.score}}</td>
          <td class="text-truncate">{{Data.percent}}</td>

       </tr>
     </tbody>
   </table>

component.ts

sortTable(value) {
    if (this.order === value) {
      this.reverse = !this.reverse;
    }
    this.order = value;
  }
VadimDez commented 5 years ago

I think I might have an answer for this. It's little bit tricky.

Given:

const collection = [
  {
    "name": "Mark",
    "age" : "25"
  },
  {
    "name": "Peter",
    "age" : "25"
  },
  {
    "name": "John ",
    "age" : "25"
  }
];

So if you sort collection by name you'll get: John, Mark, Peter. And then if you sort by age you'll get: Mark, Peter, John and not John, Mark, Peter! The reason for that is that instead of taking last result (which is sorted asJohn, Mark, Peter) pipe will take original array (collection, which is sorted as Mark, Peter, John).

Hope that is clear.