Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort my data by name in a *NgFor list?

I am working on a new project on Angular 6.

I have a list of students with a *NgFor :

(html)

<tr *ngFor="let kk of Repdata | filterdata: queryString : 'name' ; let ind = index">

    <td>{{ind + 1}}</td>
    <td>{{kk.name}}</td>
    <td>{{kk.prenom}}</td>

(filterdata is use for a seachbar)

typescript :

GetUser(){
   return this.http.get('http://localhost:8080/api/getUser/')
           .map((response: Response) => response.json())
}

javascript :

app.get("/api/getUser",function(req,res){
    model.find({},function(err,data){
              if(err){
                  res.send(err);
              }
              else{
                  res.send(data);
                  }
          });
  })

In my table, I have my data ordered by id, but I want to order it by name. I don't know how to.

like image 278
WeelCodeur Avatar asked Jan 30 '26 16:01

WeelCodeur


1 Answers

if you just want your data sorted by name outside of the Database you can use the sort() function as explained here Sort array by firstname (alphabetically) in Javascript

you could also use the Sort function provided by mongodb as it seems you re using mongoose. https://docs.mongodb.com/manual/reference/method/cursor.sort/

if u want to just sort it in the Angular view you can use a custome sort pipe as explained here: Angular 2 OrderBy Pipe

edit:

You shouldnt use Pipes for Filtering / Sorting as they are very unperformant as stated here: https://angular.io/guide/pipes#appendix-no-filterpipe-or-orderbypipe

like image 63
Michel Vorwieger Avatar answered Feb 01 '26 07:02

Michel Vorwieger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!