Sort the items of the list in place
(
mc: &'gc Mutation<'gc>,
receiver: Value<'gc>,
args: Vec<Value<'gc>>,
)
| 306 | |
| 307 | // Sort the items of the list in place |
| 308 | fn sort<'gc>( |
| 309 | mc: &'gc Mutation<'gc>, |
| 310 | receiver: Value<'gc>, |
| 311 | args: Vec<Value<'gc>>, |
| 312 | ) -> Result<Value<'gc>, VmError> { |
| 313 | let list = receiver.as_array()?; |
| 314 | |
| 315 | // Check for optional reverse parameter |
| 316 | let reverse = if !args.is_empty() { |
| 317 | args[0].as_boolean() |
| 318 | } else { |
| 319 | false |
| 320 | }; |
| 321 | |
| 322 | let mut list_mut = list.borrow_mut(mc); |
| 323 | |
| 324 | // Currently only supporting numeric sorts |
| 325 | // This could be expanded to support custom comparators |
| 326 | if reverse { |
| 327 | list_mut.data.sort_by(|a, b| { |
| 328 | if let (Value::Number(x), Value::Number(y)) = (a, b) { |
| 329 | y.partial_cmp(x).unwrap() |
| 330 | } else { |
| 331 | // For non-numeric values, just keep their order |
| 332 | std::cmp::Ordering::Equal |
| 333 | } |
| 334 | }); |
| 335 | } else { |
| 336 | list_mut.data.sort_by(|a, b| { |
| 337 | if let (Value::Number(x), Value::Number(y)) = (a, b) { |
| 338 | x.partial_cmp(y).unwrap() |
| 339 | } else { |
| 340 | // For non-numeric values, just keep their order |
| 341 | std::cmp::Ordering::Equal |
| 342 | } |
| 343 | }); |
| 344 | } |
| 345 | |
| 346 | Ok(receiver) |
| 347 | } |
| 348 | |
| 349 | // Reverse the elements of the list in place |
| 350 | fn reverse<'gc>( |
nothing calls this directly
no test coverage detected