1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
use nu_engine::ClosureEval;
use nu_protocol::shell_error::generic::GenericError;
use nu_protocol::{PipelineData, Record, ShellError, Span, Value, ast::CellPath};
use nu_utils::IgnoreCaseExt;
use std::cmp::Ordering;
/// A specification of sort order for `sort_by`.
///
/// A closure comparator allows the user to return custom ordering to sort by.
/// A cell path comparator uses the value referred to by the cell path as the sorting key.
pub enum Comparator {
KeyClosure(ClosureEval),
CustomClosure(ClosureEval),
CellPath(CellPath),
}
/// Sort a slice of `Value`s.
///
/// Sort has the following invariants, in order of precedence:
/// - Null values (Nothing type) are always sorted to the end.
/// - For natural sort, numeric values (numeric strings, ints, and floats) appear first, sorted by numeric value
/// - Values appear by order of `Value`'s `PartialOrd`.
/// - Sorting for values with equal ordering is stable.
///
/// Generally, values of different types are ordered by order of appearance in the `Value` enum.
/// However, this is not always the case. For example, ints and floats will be grouped together since
/// `Value`'s `PartialOrd` defines a non-decreasing ordering between non-decreasing integers and floats.
pub fn sort(vec: &mut [Value], insensitive: bool, natural: bool) -> Result<(), ShellError> {
// allow the comparator function to indicate error
// by mutating this option captured by the closure,
// since sort_by closure must be infallible
let mut compare_err: Option<ShellError> = None;
vec.sort_by(|a, b| {
// we've already hit an error, bail out now
if compare_err.is_some() {
return Ordering::Equal;
}
compare_values(a, b, insensitive, natural).unwrap_or_else(|err| {
compare_err.get_or_insert(err);
Ordering::Equal
})
});
if let Some(err) = compare_err {
Err(err)
} else {
Ok(())
}
}
/// Sort a slice of `Value`s by criteria specified by one or multiple `Comparator`s.
pub fn sort_by(
vec: &mut [Value],
mut comparators: Vec<Comparator>,
head_span: Span,
insensitive: bool,
natural: bool,
) -> Result<(), ShellError> {
if comparators.is_empty() {
return Err(ShellError::Generic(GenericError::new(
"expected name",
"requires a cell path or closure to sort data",
head_span,
)));
}
// allow the comparator function to indicate error
// by mutating this option captured by the closure,
// since sort_by closure must be infallible
let mut compare_err: Option<ShellError> = None;
vec.sort_by(|a, b| {
compare_by(
a,
b,
&mut comparators,
head_span,
insensitive,
natural,
&mut compare_err,
)
});
if let Some(err) = compare_err {
Err(err)
} else {
Ok(())
}
}
/// Sort a record's key-value pairs.
///
/// Can sort by key or by value.
pub fn sort_record(
record: Record,
sort_by_value: bool,
reverse: bool,
insensitive: bool,
natural: bool,
) -> Result<Record, ShellError> {
let mut input_pairs: Vec<(String, Value)> = record.into_iter().collect();
// allow the comparator function to indicate error
// by mutating this option captured by the closure,
// since sort_by closure must be infallible
let mut compare_err: Option<ShellError> = None;
if sort_by_value {
input_pairs.sort_by(|a, b| {
// we've already hit an error, bail out now
if compare_err.is_some() {
return Ordering::Equal;
}
compare_values(&a.1, &b.1, insensitive, natural).unwrap_or_else(|err| {
compare_err.get_or_insert(err);
Ordering::Equal
})
});
} else {
input_pairs.sort_by(|a, b| compare_strings(&a.0, &b.0, insensitive, natural));
};
if let Some(err) = compare_err {
return Err(err);
}
if reverse {
input_pairs.reverse()
}
Ok(input_pairs.into_iter().collect())
}
pub fn compare_by(
left: &Value,
right: &Value,
comparators: &mut [Comparator],
span: Span,
insensitive: bool,
natural: bool,
error: &mut Option<ShellError>,
) -> Ordering {
// we've already hit an error, bail out now
if error.is_some() {
return Ordering::Equal;
}
for cmp in comparators.iter_mut() {
let result = match cmp {
Comparator::CellPath(cell_path) => {
compare_cell_path(left, right, cell_path, insensitive, natural)
}
Comparator::KeyClosure(closure) => {
compare_key_closure(left, right, closure, span, insensitive, natural)
}
Comparator::CustomClosure(closure) => {
compare_custom_closure(left, right, closure, span)
}
};
match result {
Ok(Ordering::Equal) => {}
Ok(ordering) => return ordering,
Err(err) => {
// don't bother continuing through the remaining comparators as we've hit an error
// don't overwrite if there's an existing error
error.get_or_insert(err);
return Ordering::Equal;
}
}
}
Ordering::Equal
}
/// Determines whether a value should be sorted as a string
///
/// If we're natural sorting, we want to sort strings, integers, and floats alphanumerically, so we should string sort.
/// Otherwise, we only want to string sort if both values are strings or globs (to enable case insensitive comparison)
fn should_sort_as_string(val: &Value, natural: bool) -> bool {
matches!(
(val, natural),
(&Value::String { .. }, _)
| (&Value::Glob { .. }, _)
| (&Value::Int { .. }, true)
| (&Value::Float { .. }, true)
)
}
/// Simple wrapper around `should_sort_as_string` to determine if two values
/// should be compared as strings.
fn should_string_compare(left: &Value, right: &Value, natural: bool) -> bool {
should_sort_as_string(left, natural) && should_sort_as_string(right, natural)
}
pub fn compare_values(
left: &Value,
right: &Value,
insensitive: bool,
natural: bool,
) -> Result<Ordering, ShellError> {
if should_string_compare(left, right, natural) {
Ok(compare_strings(
&left.coerce_str()?,
&right.coerce_str()?,
insensitive,
natural,
))
} else {
match (left, right) {
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
Ok(lhs.total_cmp(rhs))
}
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
Ok((*lhs as f64).total_cmp(rhs))
}
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
Ok(lhs.total_cmp(&(*rhs as f64)))
}
_ => Ok(left.partial_cmp(right).unwrap_or(Ordering::Equal)),
}
}
}
pub fn compare_strings(left: &str, right: &str, insensitive: bool, natural: bool) -> Ordering {
fn compare_inner<T>(left: T, right: T, natural: bool) -> Ordering
where
T: AsRef<str> + Ord,
{
if natural {
alphanumeric_sort::compare_str(left, right)
} else {
left.cmp(&right)
}
}
// only allocate a String if necessary for case folding
if insensitive {
compare_inner(left.to_folded_case(), right.to_folded_case(), natural)
} else {
compare_inner(left, right, natural)
}
}
pub fn compare_cell_path(
left: &Value,
right: &Value,
cell_path: &CellPath,
insensitive: bool,
natural: bool,
) -> Result<Ordering, ShellError> {
let left = left.follow_cell_path(&cell_path.members)?;
let right = right.follow_cell_path(&cell_path.members)?;
compare_values(&left, &right, insensitive, natural)
}
pub fn compare_key_closure(
left: &Value,
right: &Value,
closure_eval: &mut ClosureEval,
span: Span,
insensitive: bool,
natural: bool,
) -> Result<Ordering, ShellError> {
let left_key = closure_eval
.run_with_value(left.clone())?
.into_value(span)?;
let right_key = closure_eval
.run_with_value(right.clone())?
.into_value(span)?;
compare_values(&left_key, &right_key, insensitive, natural)
}
pub fn compare_custom_closure(
left: &Value,
right: &Value,
closure_eval: &mut ClosureEval,
span: Span,
) -> Result<Ordering, ShellError> {
closure_eval
.add_arg(left.clone())
.add_arg(right.clone())
.run_with_input(PipelineData::value(
Value::list(vec![left.clone(), right.clone()], span),
None,
))
.and_then(|data| data.into_value(span))
.map(|val| {
if val.is_true() {
Ordering::Less
} else {
Ordering::Greater
}
})
}
#[cfg(test)]
mod tests {
use super::*;
use nu_protocol::{ast::PathMember, casing::Casing, record};
#[test]
fn test_sort_floats_strict() {
// Values that are equal under epsilon tolerance but different strictly
let a = Value::test_float(7.0);
let b = Value::test_float(7.0 + 0.6 * f64::EPSILON * 7.0);
let c = Value::test_float(7.0 + 1.2 * f64::EPSILON * 7.0);
// Under epsilon tolerance:
// a == b (diff = 0.6 * EPS * 7.0 < prec = 1.0 * EPS * 7.0)
// b == c (diff = 0.6 * EPS * 7.0 < prec = 1.0... * EPS * 7.0)
// a < c (diff = 1.2 * EPS * 7.0 > prec = 1.0 * EPS * 7.0)
// This non-transitivity causes panics in sort.
let mut list = vec![c.clone(), b.clone(), a.clone()];
// This should not panic now and should sort strictly
assert!(sort(&mut list, false, false).is_ok());
assert_eq!(list, vec![a, b, c]);
}
#[test]
fn test_sort_basic() {
let mut list = vec![
Value::test_string("foo"),
Value::test_int(2),
Value::test_int(3),
Value::test_string("bar"),
Value::test_int(1),
Value::test_string("baz"),
];
assert!(sort(&mut list, false, false).is_ok());
assert_eq!(
list,
vec![
Value::test_int(1),
Value::test_int(2),
Value::test_int(3),
Value::test_string("bar"),
Value::test_string("baz"),
Value::test_string("foo")
]
);
}
#[test]
fn test_sort_nothing() {
// Nothing values should always be sorted to the end of any list
let mut list = vec![
Value::test_int(1),
Value::test_nothing(),
Value::test_int(2),
Value::test_string("foo"),
Value::test_nothing(),
Value::test_string("bar"),
];
assert!(sort(&mut list, false, false).is_ok());
assert_eq!(
list,
vec![
Value::test_int(1),
Value::test_int(2),
Value::test_string("bar"),
Value::test_string("foo"),
Value::test_nothing(),
Value::test_nothing()
]
);
// Ensure that nothing values are sorted after *all* types,
// even types which may follow `Nothing` in the PartialOrd order
// unstable_name_collision
// can be switched to std intersperse when stabilized
let mut values: Vec<Value> =
itertools::intersperse(Value::test_values(), Value::test_nothing()).collect();
let nulls = values
.iter()
.filter(|item| item == &&Value::test_nothing())
.count();
assert!(sort(&mut values, false, false).is_ok());
// check if the last `nulls` values of the sorted list are indeed null
assert_eq!(&values[(nulls - 1)..], vec![Value::test_nothing(); nulls])
}
#[test]
fn test_sort_natural_basic() {
let mut list = vec![
Value::test_string("foo99"),
Value::test_string("foo9"),
Value::test_string("foo1"),
Value::test_string("foo100"),
Value::test_string("foo10"),
Value::test_string("1"),
Value::test_string("10"),
Value::test_string("100"),
Value::test_string("9"),
Value::test_string("99"),
];
assert!(sort(&mut list, false, false).is_ok());
assert_eq!(
list,
vec![
Value::test_string("1"),
Value::test_string("10"),
Value::test_string("100"),
Value::test_string("9"),
Value::test_string("99"),
Value::test_string("foo1"),
Value::test_string("foo10"),
Value::test_string("foo100"),
Value::test_string("foo9"),
Value::test_string("foo99"),
]
);
assert!(sort(&mut list, false, true).is_ok());
assert_eq!(
list,
vec![
Value::test_string("1"),
Value::test_string("9"),
Value::test_string("10"),
Value::test_string("99"),
Value::test_string("100"),
Value::test_string("foo1"),
Value::test_string("foo9"),
Value::test_string("foo10"),
Value::test_string("foo99"),
Value::test_string("foo100"),
]
);
}
#[test]
fn test_sort_natural_mixed_types() {
let mut list = vec![
Value::test_string("1"),
Value::test_int(99),
Value::test_int(1),
Value::test_float(1000.0),
Value::test_int(9),
Value::test_string("9"),
Value::test_int(100),
Value::test_string("99"),
Value::test_float(2.0),
Value::test_string("100"),
Value::test_int(10),
Value::test_string("10"),
];
assert!(sort(&mut list, false, false).is_ok());
assert_eq!(
list,
vec![
Value::test_int(1),
Value::test_float(2.0),
Value::test_int(9),
Value::test_int(10),
Value::test_int(99),
Value::test_int(100),
Value::test_float(1000.0),
Value::test_string("1"),
Value::test_string("10"),
Value::test_string("100"),
Value::test_string("9"),
Value::test_string("99")
]
);
assert!(sort(&mut list, false, true).is_ok());
assert_eq!(
list,
vec![
Value::test_int(1),
Value::test_string("1"),
Value::test_float(2.0),
Value::test_int(9),
Value::test_string("9"),
Value::test_int(10),
Value::test_string("10"),
Value::test_int(99),
Value::test_string("99"),
Value::test_int(100),
Value::test_string("100"),
Value::test_float(1000.0),
]
);
}
#[test]
fn test_sort_natural_no_numeric_values() {
// If list contains no numeric strings, it should be sorted the
// same with or without natural sorting
let mut normal = vec![
Value::test_string("golf"),
Value::test_bool(false),
Value::test_string("alfa"),
Value::test_string("echo"),
Value::test_int(7),
Value::test_int(10),
Value::test_bool(true),
Value::test_string("uniform"),
Value::test_int(3),
Value::test_string("tango"),
];
let mut natural = normal.clone();
assert!(sort(&mut normal, false, false).is_ok());
assert!(sort(&mut natural, false, true).is_ok());
assert_eq!(normal, natural);
}
#[test]
fn test_sort_natural_type_order() {
// This test is to prevent regression to a previous natural sort behavior
// where values of different types would be intermixed.
// Only numeric values (ints, floats, and numeric strings) should be intermixed
//
// This list would previously be incorrectly sorted like this:
// â•────┬─────────╮
// │ 0 │ 1 │
// │ 1 │ golf │
// │ 2 │ false │
// │ 3 │ 7 │
// │ 4 │ 10 │
// │ 5 │ alfa │
// │ 6 │ true │
// │ 7 │ uniform │
// │ 8 │ true │
// │ 9 │ 3 │
// │ 10 │ false │
// │ 11 │ tango │
// ╰────┴─────────╯
let mut list = vec![
Value::test_string("golf"),
Value::test_int(1),
Value::test_bool(false),
Value::test_string("alfa"),
Value::test_int(7),
Value::test_int(10),
Value::test_bool(true),
Value::test_string("uniform"),
Value::test_bool(true),
Value::test_int(3),
Value::test_bool(false),
Value::test_string("tango"),
];
assert!(sort(&mut list, false, true).is_ok());
assert_eq!(
list,
vec![
Value::test_bool(false),
Value::test_bool(false),
Value::test_bool(true),
Value::test_bool(true),
Value::test_int(1),
Value::test_int(3),
Value::test_int(7),
Value::test_int(10),
Value::test_string("alfa"),
Value::test_string("golf"),
Value::test_string("tango"),
Value::test_string("uniform")
]
);
// Only ints, floats, and numeric strings should be intermixed
// While binary primitives and datetimes can be coerced into strings, it doesn't make sense to sort them with numbers
// Binary primitives can hold multiple values, not just one, so shouldn't be compared to single values
// Datetimes don't have a single obvious numeric representation, and if we chose one it would be ambiguous to the user
let year_three = chrono::NaiveDate::from_ymd_opt(3, 1, 1)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc();
let mut list = vec![
Value::test_int(10),
Value::test_float(6.0),
Value::test_int(1),
Value::test_binary([3]),
Value::test_string("2"),
Value::test_date(year_three.into()),
Value::test_int(4),
Value::test_binary([52]),
Value::test_float(9.0),
Value::test_string("5"),
Value::test_date(chrono::DateTime::UNIX_EPOCH.into()),
Value::test_int(7),
Value::test_string("8"),
Value::test_float(3.0),
Value::test_string("foobar"),
];
assert!(sort(&mut list, false, true).is_ok());
assert_eq!(
list,
vec![
Value::test_int(1),
Value::test_string("2"),
Value::test_float(3.0),
Value::test_int(4),
Value::test_string("5"),
Value::test_float(6.0),
Value::test_int(7),
Value::test_string("8"),
Value::test_float(9.0),
Value::test_int(10),
Value::test_string("foobar"),
// the ordering of date and binary here may change if the PartialOrd order is changed,
// but they should not be intermixed with the above
Value::test_date(year_three.into()),
Value::test_date(chrono::DateTime::UNIX_EPOCH.into()),
Value::test_binary([3]),
Value::test_binary([52]),
]
);
}
#[test]
fn test_sort_insensitive() {
// Test permutations between insensitive and natural
// Ensure that strings with equal insensitive orderings
// are sorted stably. (FOO then foo, bar then BAR)
let source = vec![
Value::test_string("FOO"),
Value::test_string("foo"),
Value::test_int(100),
Value::test_string("9"),
Value::test_string("bar"),
Value::test_int(10),
Value::test_string("baz"),
Value::test_string("BAR"),
];
let mut list;
// sensitive + non-natural
list = source.clone();
assert!(sort(&mut list, false, false).is_ok());
assert_eq!(
list,
vec![
Value::test_int(10),
Value::test_int(100),
Value::test_string("9"),
Value::test_string("BAR"),
Value::test_string("FOO"),
Value::test_string("bar"),
Value::test_string("baz"),
Value::test_string("foo"),
]
);
// sensitive + natural
list = source.clone();
assert!(sort(&mut list, false, true).is_ok());
assert_eq!(
list,
vec![
Value::test_string("9"),
Value::test_int(10),
Value::test_int(100),
Value::test_string("BAR"),
Value::test_string("FOO"),
Value::test_string("bar"),
Value::test_string("baz"),
Value::test_string("foo"),
]
);
// insensitive + non-natural
list = source.clone();
assert!(sort(&mut list, true, false).is_ok());
assert_eq!(
list,
vec![
Value::test_int(10),
Value::test_int(100),
Value::test_string("9"),
Value::test_string("bar"),
Value::test_string("BAR"),
Value::test_string("baz"),
Value::test_string("FOO"),
Value::test_string("foo"),
]
);
// insensitive + natural
list = source.clone();
assert!(sort(&mut list, true, true).is_ok());
assert_eq!(
list,
vec![
Value::test_string("9"),
Value::test_int(10),
Value::test_int(100),
Value::test_string("bar"),
Value::test_string("BAR"),
Value::test_string("baz"),
Value::test_string("FOO"),
Value::test_string("foo"),
]
);
}
// Helper function to assert that two records are equal
// with their key-value pairs in the same order
fn assert_record_eq(a: Record, b: Record) {
assert_eq!(
a.into_iter().collect::<Vec<_>>(),
b.into_iter().collect::<Vec<_>>(),
)
}
#[test]
fn test_sort_record_keys() {
// Basic record sort test
let record = record! {
"golf" => Value::test_string("bar"),
"alfa" => Value::test_string("foo"),
"echo" => Value::test_int(123),
};
let sorted = sort_record(record, false, false, false, false).unwrap();
assert_record_eq(
sorted,
record! {
"alfa" => Value::test_string("foo"),
"echo" => Value::test_int(123),
"golf" => Value::test_string("bar"),
},
);
}
#[test]
fn test_sort_record_values() {
// This test is to prevent a regression where integers and strings would be
// intermixed non-naturally when sorting a record by value without the natural flag:
//
// This record would previously be incorrectly sorted like this:
// â•─────────┬─────╮
// │ alfa │ 1 │
// │ charlie │ 1 │
// │ india │ 10 │
// │ juliett │ 10 │
// │ foxtrot │ 100 │
// │ hotel │ 100 │
// │ delta │ 9 │
// │ echo │ 9 │
// │ bravo │ 99 │
// │ golf │ 99 │
// ╰─────────┴─────╯
let record = record! {
"alfa" => Value::test_string("1"),
"bravo" => Value::test_int(99),
"charlie" => Value::test_int(1),
"delta" => Value::test_int(9),
"echo" => Value::test_string("9"),
"foxtrot" => Value::test_int(100),
"golf" => Value::test_string("99"),
"hotel" => Value::test_string("100"),
"india" => Value::test_int(10),
"juliett" => Value::test_string("10"),
};
// non-natural sort
let sorted = sort_record(record.clone(), true, false, false, false).unwrap();
assert_record_eq(
sorted,
record! {
"charlie" => Value::test_int(1),
"delta" => Value::test_int(9),
"india" => Value::test_int(10),
"bravo" => Value::test_int(99),
"foxtrot" => Value::test_int(100),
"alfa" => Value::test_string("1"),
"juliett" => Value::test_string("10"),
"hotel" => Value::test_string("100"),
"echo" => Value::test_string("9"),
"golf" => Value::test_string("99"),
},
);
// natural sort
let sorted = sort_record(record.clone(), true, false, false, true).unwrap();
assert_record_eq(
sorted,
record! {
"alfa" => Value::test_string("1"),
"charlie" => Value::test_int(1),
"delta" => Value::test_int(9),
"echo" => Value::test_string("9"),
"india" => Value::test_int(10),
"juliett" => Value::test_string("10"),
"bravo" => Value::test_int(99),
"golf" => Value::test_string("99"),
"foxtrot" => Value::test_int(100),
"hotel" => Value::test_string("100"),
},
);
}
#[test]
fn test_sort_equivalent() {
// Ensure that sort, sort_by, and record sort have equivalent sorting logic
let phonetic = vec![
"alfa", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel", "india",
"juliett", "kilo", "lima", "mike", "november", "oscar", "papa", "quebec", "romeo",
"sierra", "tango", "uniform", "victor", "whiskey", "xray", "yankee", "zulu",
];
// filter out errors, since we can't sort_by on those
let mut values: Vec<Value> = Value::test_values()
.into_iter()
.filter(|val| !matches!(val, Value::Error { .. }))
.collect();
// reverse sort test values
values.sort_by(|a, b| b.partial_cmp(a).unwrap());
let mut list = values.clone();
let mut table: Vec<Value> = values
.clone()
.into_iter()
.map(|val| Value::test_record(record! { "value" => val }))
.collect();
let record = Record::from_iter(phonetic.into_iter().map(str::to_string).zip(values));
let comparator = Comparator::CellPath(CellPath {
members: vec![PathMember::String {
val: "value".to_string(),
span: Span::test_data(),
optional: false,
casing: Casing::Sensitive,
}],
});
assert!(sort(&mut list, false, false).is_ok());
assert!(
sort_by(
&mut table,
vec![comparator],
Span::test_data(),
false,
false
)
.is_ok()
);
let record_sorted = sort_record(record.clone(), true, false, false, false).unwrap();
let record_vals: Vec<Value> = record_sorted.into_iter().map(|pair| pair.1).collect();
let table_vals: Vec<Value> = table
.clone()
.into_iter()
.map(|record| record.into_record().unwrap().remove("value").unwrap())
.collect();
assert_eq!(list, record_vals);
assert_eq!(record_vals, table_vals);
// list == table_vals by transitive property
}
}