arrays - Ajax DATA changes sorting -


im doing ajax call wordpress site. works perfect except order (sort) of data coming out success function.

if print_r ($service) validate correct custom order want

but after send using:

(php file)

public function closest_services() {      global $wpdb;      $servicios_repetidos = $_post['servicios_repetidos'];      $lat        = $_post['latitud'];     $lng        = $_post['longitud'];     $categoria  = array();     $unit       = $_post['unit'];     $distancia  = $_post['radius'];      if ( $unit == 'km' ) { $earth_radius = 6371.009 / 1.609344 ; }     elseif ( $unit == 'mi' ) { $earth_radius = 3958.761; }      $sql = $wpdb->prepare( "         select distinct             p.id service_id,             p.post_title service_title,             map_lat.meta_value loclat,             map_lng.meta_value loclong,               %f * 2 * asin(sqrt( power(sin(( %f - map_lat.meta_value ) * pi()/180 / 2), 2)              + cos( %f * pi()/180) * cos( map_lng.meta_value * pi()/180)              * power(sin(( %f - map_lng.meta_value) * pi()/180 / 2), 2) ))              distance          $wpdb->posts p         inner join $wpdb->postmeta map_lat on p.id = map_lat.post_id         inner join $wpdb->postmeta map_lng on p.id = map_lng.post_id         1 = 1         , p.post_type = 'servicios'         , p.post_status = 'publish'         , map_lat.meta_key = 'et_meta_latitud'         , map_lng.meta_key = 'et_meta_longitud'         having distance <= %d         order distance desc",         $earth_radius,         $lat,         $lat,         $lng,         $distancia           );      $closest_services_query = $wpdb->get_results( $sql );     $servicio = array();      foreach ( $closest_services_query $single_service ):          $servicio[$single_service->service_id]["service_id"]            = $single_service->service_id;         $servicio[$single_service->service_id]["meta_fields"]           = get_post_custom( $single_service->service_id );         $servicio[$single_service->service_id]["service_author"]        = get_post_field ( 'post_author' , $single_service->service_id );         $servicio[$single_service->service_id]["service_author_avatar"] = get_avatar( $single_service->service_id , '64');         $servicio[$single_service->service_id]["service_author_url"]    = bp_core_get_user_domain( $single_service->service_id );                    $servicio[$single_service->service_id]["category"]              = get_the_terms( $single_service->service_id , 'servicios-categoria');         $servicio[$single_service->service_id]["service_title"]         = $single_service->service_title;          $servicio[$single_service->service_id]["thumb_url"]             = wp_get_attachment_image_src( get_post_thumbnail_id( $single_service->service_id ) , 'medium' );          $servicio[$single_service->service_id]["distancia"]             = $single_service->distance;          $servicio[$single_service->service_id]["precio_servicio_visibilidad"] = get_post_meta( $single_service->service_id , 'et_meta_precio_visibilidad' , true);          if ( function_exists( 'get_favorites_button' ) )              $servicio[$single_service->service_id]["favorite_button"] = get_favorites_button( $single_service->service_id );      endforeach;      //print_r($servicio);     wp_send_json($servicio);  } 

(jquery file)

function ajax_mapa () {                      var center = map.getcenter();      latitud     = center.lat();     longitud    = center.lng();      jquery( "body" ).after( "<div class='loading'>loading&#8230;</div>" );      jquery.ajax({         url: ajax_object.ajax_url,         type: "post",         datatype: "json",          data: {              'action'    : 'closest_services',             'latitud'   : latitud,             'longitud'  : longitud,             'categoria' : categoria,             'servicios_repetidos' : servicios_repetidos,             'unit'      : 'km',             'radius'    : 15         },                success: function( data, textstatus, jqxhr ) { // si todo salio bien se ejecuta esto              console.log(data);             console.log(textstatus);             console.log(jqxhr);              var servicios = [];             var precio;             var moneda;             var url_imangen;             var html_marker;                jquery.each( data, function( id_servicio, info_servicio ) { ... ...});      } ... ...      ...  }) ...    } ... 

the log data shows object ordered key value instead custom order send before.

if check log jqxhr.responsetext have right order want, data being received fine, changing order.

so, question is, knows why data being sorted key value, , how can fix this?

(log of jqxhr correct order - distance )

"{"144":

{"service_id":"144","distancia":"6.043960511486238"},"110":{"service_id":"110","distancia":"6.040024571156195"},"112":{"service_id":"112","distancia":"6.040024571156195"},"113":{"service_id":"113","distancia":"6.040024571156195"},"114":{"service_id":"114","distancia":"6.040024571156195"},"119":{"service_id":"119","distancia":"6.040024571156195"},"145":{"service_id":"145","distancia":"6.040024571156195"},"194":{"service_id":"194","distancia":"5.025501693483518"},"163":{"service_id":"163","distancia":"4.985331564912346"},"161":{"service_id":"161","distancia":"1.3274942401635288"}}"

(log of data incorrect order - key id )

object { 110: object, 112: object, 113: object, 114: object, 119: object, 144: object, 145: object, 161: object, 163: object, 194: object }

thanks

don't sure if best solution, manage make work converting object array , sorting array.

basically added code:

/* ********************** */ // ordeno por distancia /* ********************** */ var arr = jquery.map(data, function(value, key){     return value });  function sortbyname(a, b){     var aname = a.distancia;     var bname = b.distancia;       return ((aname > bname) ? -1 : ((aname < bname) ? 1 : 0)); }  arr.sort(sortbyname); /* ********************** */ 

just before de jquery.each( arr , function( id_servicio, info_servicio ) { ... ...}); function


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -