Sorting Columns in a Grid When Order isn't Clear

Suppose I query a database table for some data and display it in a table where I can set the sorting of each column. Here are three scenarios where the data either doesn't have an obvious order, or what we display is not in the database:

Case 1: Suppose we store usernames and not full names, but we only display full names in the table. Is there an easy way of being able to sort the table based on the full name without having to also save the full name in the database?

Case 2: Supposed we store two numerical values x and y in the database, and we display x/y. How can I sort by the value of x/y?

Case 3: Suppose that we have some text values each associated with an id, but that the id (for whatever reasons) cannot be used to order the text by significance. Is there a way to sort the text in some predetermined but custom order, without having to save information in the database about what this custom order is?

  Discussion posts and replies are publicly visible

Parents
  • +1
    Certified Lead Developer
    1. Possible? yes.  Easy? not really.  It requires a workaround, along the lines of querying the whole table into one local variable, iterating over that array to create a second "translated" local varaible, then creating a sorted data subset from that.  Obviously this requires it to be a fairly limited data set (no more than a few hundred items at the largest, preferably).
      My preferred method, however, is that if the column is sorted on username, then display the full name along with the username (rich text grids FTW) in a way that will make sense to the user.

    2. When you say "x/y", do you mean literally the string of "x", & the "/" sign, then "y", or do you mean you'd be displaying "x divided by y"?  In that case, you might be able to accomplish the heavy lifting necessary using a view.

    3. My suggested approach for this is to also add an "Order ID" column, which is not the primary key ID (and not related to it), but just an ID strictly used for ordering of items in the table.
  • I did mean x divided by y, thank you.

    The issue I have is that i need to be able to handle all of these cases in the same grid, and preferably still using the grid pagination, in which case I think that I cannot easily just translate the data in case 1, since then I wouldn't be able to move to the next page and keep the sorting. 

    Thank you for your answer though! 

  • 0
    Certified Lead Developer
    in reply to dannyh0001

    Gotcha - then I second my previous suggestion that you do this by creating a View.  Additionally I've found it's almost always a good idea to keep a "Users" table that mimics the user account information (you need to manually sync this, but doing so isn't very hard) -- and having this would allow you to do various different possibilities in your view, such as having columns for first name, last name, full name, etc, and you could potentially sort on any of those per your need.

Reply Children
No Data