What is the non-WTF way to implement a sorting feature for a forum like webapp that uses SpringFramework?



  • Continuing the discussion from Do you ever sort a b-tree(or whatever) in your daily job?:

    @asdf said:

    @Tsaukpaetra said:
    👋 Never came up in any lecture. Though to be fair I never did take any theoretical programming concepts courses....

    Really? I just checked: B-trees were mentioned in at least 3 different mandatory courses I had to take for my B.Sc. (Introduction to Algorithms, Introduction to Databases and Operating Systems). But yeah, I guess it's not mandatory knowledge, since B-trees are a very specific data structure that is only used under very specific circumstances.

    @Ascendant said:

    Yup. That's what I get told to do in every project.

    :facepalm:

    Please forget everything any of your bosses or coworkers ever told you. Think about your future coworkers' sanity!

    The kind of projects I usually work on is, I think, basically a forum.
    I've been using Spring Framework, iBatis, Hibernate, jsp for display and JQuery/UI.

    But, (always a but) of all the dozen projects I worked on, I've never seen my senior implement a sort feature, nor have I ever implemented one.

    Basically a sort feature was never required or implemented with an ORDER BY clause at the database.

    So my question is, what is the non-WTF way of implementing a sort feature?

    For example, do I SELECT 100 records from the database and sort them with javascript/JQueryUI?



  • Use an ORDER BY clause. The end.



  • @MathNerdCNU said:

    Use an ORDER BY clause. The end.

    Awww. You are just like my seniors! 😛



  • I thought your question was what the non-WTF way to sort a data set was? If your storage engine comes with a sort capability um...use it?

    If you want to sperg-out like a vendor I alluded to in the status thread then put on a helmet and run into a brick wall as fast as you can 10 times.



  • @MathNerdCNU said:

    If your storage engine comes with a sort capability um...use it?

    So, it's okay to use an ORDER BY clause to sort records?

    Hmm... Okay.



  • Depends on your sort criteria.
    Sorting by date? Use the fucking ORDER BY.

    Sorting by last name? ORDER BY might give you weird/non-intuitive results for OCONNOR vs O'CONNOR vs O'CONOR.



  • Okay, well that makes my job easier then.



  • Our company takes the opposite approach. REST API does not guarantee order at all. If you want order, the front-end does it. There's only one exception to that where we wrote a "order" int in the JSON output, which is listing names. (Because it's paginated, and it wouldn't make sense to paginate non-ordered content.) That's it.

    So far our front-end devs haven't complained. There's only a couple places where order is important to them anyway, so it hasn't really come-up.

    We have had one client who was pissy because our XML files didn't guarantee order in arrays, so now our XML files have established order. Which I think is stupid as fuck. But whatever, it makes a client happy.



  • @blakeyrat said:

    Our company takes the opposite approach. REST API does not guarantee order at all. If you want order, the front-end does it.

    So what do the front-end devs use to sort(some sort of javascript?) and how big is the list usually?



  • To be honest, I try to avoid touching front end stuff. They're using one of the big frameworks, I want to say Angular. So however you do it in Angular?



  • @blakeyrat said:

    To be honest, I try to avoid touching front end stuff. They're using one of the big frameworks, I want to say Angular. So however you do it in Angular?

    Yeah, front-end is not the kind of thing that makes me all excited. Fascinating area though.

    All I know about Angular is that it's Angular.js so it's javascript alright then.



  • @blakeyrat said:

    So however you do it in Angular?

    I'd assume it's however you do it in JavaScript. You don't even need DOM to get Array.prototype.sort.


  • Winner of the 2016 Presidential Election

    @Ascendant said:

    But, (always a but) of all the dozen projects I worked on, I've never seen my senior implement a sort feature, nor have I ever implemented one.

    Basically a sort feature was never required or implemented with an ORDER BY clause at the database.

    ORDER BY is completely fine if you're fetching from the database anyway. The :wtf: part in the thread you quoted was the following:

    @Jaime said:

    So... if you had data on the screen and the user wanted the data sorted by a different column, you would discard the data and fetch a new result set just to implement a sort? That's nuts.

    If you already have the data and just need to re-sort it, then either:

    • pull it from the database sorted correctly in the first place or
    • sort it manually without hitting the database again.

    Querying the database twice for the same data is stupid and inefficient. (Assuming you already fetched everything you need and not only the first x records.)


  • I survived the hour long Uno hand

    @blakeyrat said:

    (Because it's paginated, and it wouldn't make sense to paginate non-ordered content.)

    Here, we have strict instructions to paginate anything that may one day become over about 500 records or so. So we almost never sort on the client, and changing a filter or sort means a round-trip to the database. It seems horribly inefficient, but I haven't measured the trade-off of sending larger packets.



  • @ben_lubar said:

    You don't even need DOM to get Array.prototype.sort.

    No, but Array.prototype.sort doesn't work for a lot of things, since JavaScript has extremely loose typing and will frequently sort wrong due to that.


  • Discourse touched me in a no-no place

    @asdf said:

    If you already have the data and just need to re-sort it, then either:

    20 years ago, IE had the ability to bind an HTML table to data coming back from ADO or something similar--I think it was called RDF? It's been a long time, but I think it would automatically put in a header row for you and you could click on a column to sort the table by that column, entirely client side, without writing any code of your own, like you can do in some grid controls now. Very neat, but apparently nobody ever used it.


  • Discourse touched me in a no-no place

    @blakeyrat said:

    No, but Array.prototype.sort doesn't work for a lot of things, since JavaScript has extremely loose typing and will frequently sort wrong due to that.

    If it's a complicated case, you use collation keys. Or you do it server-side. (Or both.)



  • @blakeyrat said:

    Array.prototype.sort doesn't work for a lot of things, since JavaScript has extremely loose typing and will frequently sort wrong due to that.

    That's what compareFunction is for. You don't like the default sort? Write your own.


Log in to reply