Laravel order by

Asked

Viewed 76 times

-1

I’m having trouble sorting data that are organized as follows:

1
1.1
2.2
2.2.1
2.2.1.1
10
11

The result I have is:

1
1.1
10
11
2.2
2.2.1
2.2.1.1

To order I’m using:

Schedule::where( 'construction_uuid', '=', $construction_uuid )->orderBy('eap', 'ASC')->get();

What would be the best solution to my problem?

  • It seems to me that its field is of type text and, therefore, the order obtained is the expected one. Maybe you want a numerical sort and in this case you should turn your text field into a number. Note the number of digits in each level. Another possibility is, while maintaining the text field, to normalize the size of the numbers of each level (, '01.00.00.00', '01.01.00.00', ... '10.01.12.05').

1 answer

2


In this modeling you have chosen to store the value already formatted, the ideal would be to use the data semantically.

Present value

  • 1.1
  • 1.2.3
  • 1.2.3.4

The ideal format to store this information is:

  • 01001000
  • 01002000
  • 01002001
  • 01002002
  • 01002003

Use the format "AABBBCCCDDD" two initial and three subsequent boxes

Formatting the data you do via code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.