| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | |
| 5 | pub fn sortBy(comptime T: type, items: []T, comptime field: std.meta.FieldEnum(T)) void { |
| 6 | std.mem.sort(T, items, {}, extras.lessThanBy(T, field)); |
| 7 | } |
| 8 | |
| 9 | test { |
| 10 | const Block = struct { |
| 11 | from: u21, |
| 12 | to: u21, |
| 13 | name: []const u8, |
| 14 | }; |
| 15 | var data = [_]Block{ |
| 16 | .{ .from = 0x0000, .to = 0x007F, .name = "Basic Latin" }, |
| 17 | .{ .from = 0x0250, .to = 0x02AF, .name = "IPA Extensions" }, |
| 18 | .{ .from = 0x0100, .to = 0x017F, .name = "Latin Extended-A" }, |
| 19 | .{ .from = 0x0180, .to = 0x024F, .name = "Latin Extended-B" }, |
| 20 | .{ .from = 0x0080, .to = 0x00FF, .name = "Latin-1 Supplement" }, |
| 21 | }; |
| 22 | sortBy(Block, &data, .from); |
| 23 | try std.testing.expect(std.mem.eql(u8, data[0].name, "Basic Latin")); |
| 24 | try std.testing.expect(std.mem.eql(u8, data[1].name, "Latin-1 Supplement")); |
| 25 | try std.testing.expect(std.mem.eql(u8, data[2].name, "Latin Extended-A")); |
| 26 | try std.testing.expect(std.mem.eql(u8, data[3].name, "Latin Extended-B")); |
| 27 | try std.testing.expect(std.mem.eql(u8, data[4].name, "IPA Extensions")); |
| 28 | } |