How do return jsonobject instead of Map inside Rust

Discussão em 'Angular' iniciado por New User, Abril 23, 2025.

  1. New User

    New User Guest

    i am using sort function to sort JSON based on object key this is my function as below

    #[wasm_bindgen]
    pub fn sort_json_array(arr: JsValue, key: &str, ascending: bool) -> JsValue {
    // Deserialize the JsValue into a Vec<Value>
    let mut objects: Vec<Value> = from_value(arr).unwrap_or_default();

    // Sort the objects by the specified key
    objects.sort_by(|a, b| {
    let a_val = a.get(key).unwrap_or(&Value::Null);
    let b_val = b.get(key).unwrap_or(&Value::Null);

    // Perform type-aware comparison
    match (a_val, b_val) {
    (Value::String(a_str), Value::String(b_str)) => {
    if ascending {
    a_str.cmp(b_str)
    } else {
    b_str.cmp(a_str)
    }
    }
    (Value::Number(a_num), Value::Number(b_num)) => {
    if ascending {
    a_num.as_f64().partial_cmp(&b_num.as_f64()).unwrap_or(std::cmp::Ordering::Equal)
    } else {
    b_num.as_f64().partial_cmp(&a_num.as_f64()).unwrap_or(std::cmp::Ordering::Equal)
    }
    }
    (Value::Bool(a_bool), Value::Bool(b_bool)) => {
    if ascending {
    a_bool.cmp(b_bool)
    } else {
    b_bool.cmp(a_bool)
    }
    }
    _ => std::cmp::Ordering::Equal, // Default to equal for unsupported types
    }
    });

    let json_object = Value::Object(&objects);

    // Serialize the sorted objects back into JsValue
    to_value(&json_object).unwrap() //[{}]
    }


    This working fine however i m facing issues like instead of JSON object {} its returning like Map and i would like to pass additional param type if type is String i am interested in calling only string compared method and similar for number and bools to enhance time complexity.

    Returning the response like [​IMG] However expecting like [​IMG]

    Continue reading...

Compartilhe esta Página