3

Possible Duplicate:
Sorting on the server or on the client?

As a part of a project for my university, I have to make a website like a forum let's say.

So there will be posts, many of them. Every Post has a like bar, comments, some text, some buttons etc. Also the user will be able to sort these posts that appear in a page, based on some criteria like date,name, popularity etc.

My question is how should I implement the sorting: 1. with javascript on the browser's side, or 2. with a form or something, and a new request to the server (in this way the server has to send me back the posts sorted) ?

Thank you in advance.

Community
  • 1
  • 1
  • If there are no requirements do what is easiest to implement. – Jeroen Dec 11 '12 at 02:02
  • 1
    @Jeroen: I disagree. There has *never* been a period when requirements were set 100% correctly by someone else and developers just had to faithfully implement them. In modern times, we have generally given up that illusion and place quite a bit of responsibility for evolving requirements on the development team (by providing feedback as to what options are available, what works best, and the time/cost of each opion). Agile methodologies help manage that reality. – Eric J. Dec 11 '12 at 02:05
  • @Eric J. I agree. And in this case i would do what is easiest to implement. Saves time. – Jeroen Dec 11 '12 at 02:21
  • Do you use pagination, or plan to support it in near future? – Bergi Dec 11 '12 at 03:24

4 Answers4

3

There are pro's and con's to both.

Generally speaking, if you already have all of the data available in the client anyhow, you will provide a more responsive user experience sorting on the client.

If you have to fetch extra records that you would otherwise not fetch to sort client-side, there's a great chance that you are bloating the download to the client beyond the optimal point, and a sort on the server-side via Ajax would be better.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
2

That's a huge depends. Is there paging involved? What's the max size of the data set? It's only the records in the single page on client screen need to be sorted?

Server side sorting is better for:

  • Large data set
  • Faster initial page load
  • Accessibility for those not running JavaScript
  • Complex view business logic
  • Resilience to concurrent changes

Client side sorting is better for:

  • Small data set
  • Faster subsequent page loads
  • The sort criteria are user-selectable or numerous.
  • Once you have this feature, you can add filters, and pagination easily

Related question:

Sorting on the server or on the client?

Related answer:

The important thing to remember is that while balancing the load between powerful clients and the server may be a good idea in theory, only the server can maintain an index which is updated on every insert. Whatever the client does, it's starting with a non-indexed unsorted set of data.

Community
  • 1
  • 1
Tomas Ramirez Sarduy
  • 17,294
  • 8
  • 69
  • 85
  • 1
    `The sort criteria can't be expressed in pure SQL` assumes SQL on the back end, which is increasingly less true as NoSQL solutions gain in popularity. – Eric J. Dec 11 '12 at 16:27
0

if it is possible/practical to sort elements on the client side, that would bet the best solution (reduce server requests). however this is often not the case.

Justin McDonald
  • 2,156
  • 16
  • 19
0

It happened to us. We sort the data in the client side. Then a new requirement arrived. We need to put the sorted data in a report. So, instead of translating the sorted data directly into report datasource (which will be achieved through server side sorting), we are then required to catch the sort details (table to sort, column to sort, sort order) from the client side activity (on sort column event) then send it to the server when print report button is pressed then do the sorting on the serverside. Lot of work.

Bnrdo
  • 5,325
  • 3
  • 35
  • 63