Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sasafister's avatar

I don't want alphabet sorting when retrieving data from ajax

Hi all,

I don't know how to solve this, but in general, I have this chunk of code

$tournaments = Tournament::select('name', 'city', 'date', \DB::raw(sprintf(
            '(6371 * acos(cos(radians(%1$.7f)) * cos(radians(latitude)) * cos(radians(longitude) - radians(%2$.7f)) + sin(radians(%1$.7f)) * sin(radians(latitude)))) AS distance',
            $latitude,
            $longitude
            )))
        ->having('distance', '<', request('distance') ?? self::DEFAULT_DISTANCE)
        ->orderBy('distance', 'asc')
        ->get()
        ->groupBy('city');
    
    return $tournaments;

Which returns grouped cities, but the problem is when I try to get this endpoint directly it gives me sorted data like this

{
"Cakovec": [
			{ "distance": 12.906106587461018, },
			{ "distance": 12.906106587461018,}
],
"Novi Marof": [
			{ "distance": 16.303102724639714,}
],
"Sv Martin": [
			{ "distance": 23.9061609409841,}
],
"Split": [
			{"distance": 311.2904383884394,}
]
}

Which is fine, because I'm sorting this first by distance and then grouping by city, but the problem is when I try to fetch the data with http request, then 'Split' is not the last result even tough this city should be the last because of distance. Seems like browser/js or something is sorting groupby alphabetically...

Any ideas?

0 likes
20 replies
vincent15000's avatar

That's strange that you obtain two different results (about the order) with the same controller.

You have necessarily something that reorder your results.

Can you explain and show the differences and in which precise conditions ?

sasafister's avatar

@vincent15000 very strange, I have basic http request with angular

getTournaments(radius: number): Observable<Tournament[]> {
      const params = new HttpParams()
        .set('api_token', this.token)
        .set('distance', radius);

      return this.http.get<Tournament[]>(this.url, {params})
      .pipe(
        catchError(this.handleError<Tournament[]>('tournaments', []))
      )
  }

Check this out, first line (before expanding) is ok, but when I expand sorting is not okay

https://imgur.com/a/4Fds1dH

1 like
vincent15000's avatar

@sasafister That's effectively very strange. Can you check your query directly in the controller via a web route and display the result of the query with dd() ?

sasafister's avatar

@vincent15000

I did, that's fine :)

{
"Cakovec": [
			{ "distance": 12.906106587461018, },
			{ "distance": 12.906106587461018,}
],
"Novi Marof": [
			{ "distance": 16.303102724639714,}
],
"Sv Martin": [
			{ "distance": 23.9061609409841,}
],
"Split": [
			{"distance": 311.2904383884394,}
]
}
1 like
vincent15000's avatar

@sasafister Here you have the real result that is sent back from the API.

Now you should check what angular is doing with your datas.

vincent15000's avatar

@sasafister Is your routes inside the web.php file or the api.php file ?

Laravel returns automatically an array if it's an API route.

vincent15000's avatar

@sasafister Yes but you necessarily have something that disturbs the initial order. To find where it occurs, you can add some console.log() at different places in the code.

sasafister's avatar

@vincent15000 yea, I think that this can be solved with arrays and not object, but it's too much workarounds, I found this one very easy to implement.. Thnx for your assistance

1 like

Please or to participate in this conversation.