Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
Sort a paginated array

I have a paginated array and I have to sort by columns but when I sort from the current page I get the begenning or the end of the sorted array but if I change the page I get the right sorted values. It seems the current page becomes the 1st page when I trigger the function.

I use the pagination from ng-bootstrap

Function triggered when I click on a column

 sort() {
    if (this.sortAsc == false) {
      this.service.getSortedData().subscribe((data) => {
        this.array = data['data'];
        this.nextPage = data['next_page_url'];
        this.collectionSize = data['total'];
      });
    } else {
      this.service.getSortedDataDesc().subscribe((data) => {
        this.array = data['data'];
        this.nextPage = data['next_page_url'];
        this.collectionSize = data['total'];
      });
    }
  }

HTML

 <th scope="col" (click)="sort()">
              <fa-icon *ngIf="sortAsc == true" [icon]="faChevronUp"></fa-icon>
              <fa-icon *ngIf="sortAsc == false" [icon]="faChevronDown"></fa-icon>
              Values
            </th>

current result with order by desc example:

on click column from page 2 :

1000
150
20

navigate to sorted page 1 :

1000
150
20

expected result :

on click column from page 2 :

10
6
5

navigate to sorted page 1 :

1000
150
20

data content for page 2:

{
current_page: 1
data: (30) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
first_page_url: "http://localhost:8000/api/values?page=1"
from: 1
last_page: 4
last_page_url: "http://localhost:8000/api/values?page=4"
links: (6) [{…}, {…}, {…}, {…}, {…}, {…}]
next_page_url: "http://localhost:8000/api/values?page=2"
path: "http://localhost:8000/api/values"
per_page: 30
prev_page_url: null
to: 30
total: 118
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Although you havent provide the cruicial information, I'm guessing that probably the problem is in the getSortedData() and getSortedDataDesc() functions, what are they returning? You need to offset the data, somehow read current page and subtract those pages from the result passed to

this.array = data['data'];

also, you set the array in the first condition and you're not doing that in the second, maybe that's the problem?

about 4 years ago · Juan Pablo Isaza Report

0

this is because you're subscribing the value changes on a click event. try this instead:

  async sort() {
    let data = [];
    if (this.sortAsc == false) {
      data = await this.service.getSortedData().toPromise();
    } else {
      data = await this.service.getSortedDataDesc().toPromise();
    }

    this.array = data['data'];
    this.firstPageUrl = data['first_page_url'];
    this.nextPage = data['next_page_url'];
    this.collectionSize = data['total'];
  }
about 4 years ago · Juan Pablo Isaza Report

0

I found a solution, I don't think this is really clean but it works. I store the current page when I display another page then add a condition in the sort() function

    sort(){
      if (this.sortAsc == false) {
         this.service.getSortedData().subscribe((data) => {
            this.nextPage = data['next_page_url'];
            this.collectionSize = data['total'];
            if (this.currentPage == 1) {
                this.array = data['data'];
            } else {
              this.service
                .getPage(this.firstPageUrl, this.currentPage)
                .subscribe((data) => {
                         this.array = data['data'];
                });
            }
          });
        } else {
           this.service.getSortedDataDesc().subscribe((data) => {
            this.nextPage = data['next_page_url'];
            this.collectionSize = data['total'];
            if (this.currentPage == 1) {
               this.array = data['data'];
            } else {
              this.service
                .getPage(this.firstPageUrl, this.currentPage)
                .subscribe((data) => {
                         this.array = data['data'];
                });
            }
          });
        }

}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!