0

am using Laravel for some sass project where some deals are one step behind completion if just our rooms accepts ( alphabetic ) beside the actual numbers for example 101 - A101 ect.

So room_number column data type was int so i converted it to a varchar no problem until now and everything is working perfect .

Received a new request that we want to sort those rooms in a specific way so for example if i have rooms 101, 102,A101,103,B102 they must be sorted asc as 101,A101,102,B102,103

The Below Screen might give you something of what am trying to achieve

enter image description here

According to the screen room A101 must come after room 101 and room B102 must come after room 102 etc.

i tried the following

Room::where(.....)
->orderByRaw("CAST(room_number as UNSIGNED) ASC")
->get(); 

also i tried to sort the collecting after getting it

Room::where(.....)
    ->orderByRaw("CAST(room_number as UNSIGNED) ASC")
    ->get()
    ->sortBy('order', SORT_REGULAR, true); 

But all my tries came to a dead road ! am thing to perform a binary search on those rooms and apply a custom solution but i thought i could ask first maybe someone had the same issue !

7
  • This question has some answers that may be of some inspiration: stackoverflow.com/questions/47903727/… Maybe you can make the sortBy closure have the char moved to the end of the string so that it naturally sorts them after the number without any char. Sep 28, 2021 at 10:50
  • Can you add columns to your table? One for prefix ('A' or 'B' ... in this case) and one for the number (101, 102, 103...) as integer of course. So you can order them by number first and prefix second. Sep 28, 2021 at 10:55
  • @mulquin hmmm it seems a reasonable suggestion thanks Sep 28, 2021 at 10:56
  • @UğurArıcı unfortunately it is a one column room_number but wait do you mean that instead of converting the room_number column from int to varchar i leave it as int then i add a new column maybe room_number_alpha and when the user send the room number as E105 i split the incoming string and store it ?!!! please take into consideration that room_number must be unique Sep 28, 2021 at 10:59
  • @EmadRashadMuhammed yes, for this specific need you can add 2 additional columns and auto set and update them according to base and unique room_number field. When you set anything to room_number, you can set room_number_prefix to null or given string and room_number_integer to given number. You can set both of them indexes on DB. It's not the nicest but may be a solution. Sep 28, 2021 at 11:03

1 Answer 1

1

You can use either REGEXP_REPLACE() or REGEXP_SUBSTR() to extract numeric value for sort, here is a sample of how to do it

Room::where(.....)
    ->orderByRaw("REGEXP_REPLACE(room_number, '[^0-9]+', '') ASC")
    ->get();

or

Room::where(.....)
    ->orderByRaw("REGEXP_SUBSTR(room_number,'[0-9]+') ASC")
    ->get();

Note: If you need more customization consider use "ORDER BY CASE WHEN"

1
  • First one solved my problem , actually if you saw the previous comments i managed to make it work inside the sort() after getting the collection but i think your solution is faster so am gonna accept it thanks a lot Sep 28, 2021 at 11:33

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.