SuperObject(Delphi最好的JSON简析类) 扩展功能----排序(2)
在superObject中添加排序类型
type //add By Mofen
TSOSortMode = (sosmDefault {默认的方式}, sosmAdd {添加的顺序}, sosmASC {升序}, sosmDesc {降序});
添加全局设置方法
var
nowSortMode: TSOSortMode = sosmDefault;
//设置排序模式 Mofen
procedure SetSOSortMode(pvSortMode: TSOSortMode);
begin
nowSortMode := pvSortMode;
end;
需要改造的类主要为
TSuperAvlTree
其中需要改造的方法有
Insert序列化出来的顺序
Search搜索路径
Delete删除
首先主要看看Insert方法,修改该方法后,可以看到so.asJson,可以看到序列化的结果,顺序是按照我们设定的顺序排列的
1 function TSuperAvlTree.Insert(h: TSuperAvlEntry): TSuperAvlEntry;
2 var
3 unbal, parentunbal, hh, parent: TSuperAvlEntry;
4 depth, unbaldepth: longint;
5 cmp: integer;
6 unbalbf: integer;
7 branch: TSuperAvlBitArray;
8 p: Pointer;
9 begin
10 inc(FCount);
11 h.FLt := nil;
12 h.FGt := nil;
13 h.FBf := 0;
14 branch := [];
15
16 if (FRoot = nil) then
17 FRoot := h
18 else
19 begin
20 unbal := nil;
21 parentunbal := nil;
22 depth := 0;
23 unbaldepth := 0;
24 hh := FRoot;
25 parent := nil;
26 repeat
27 if (hh.FBf <> 0) then
28 begin
29 unbal := hh;
30 parentunbal := parent;
31 unbaldepth := depth;
32 end;
33 if hh.FHash <> h.FHash then
34 begin
35 if nowSortMode = sosmDefault then
36 begin
37 //original code //这里是原始的方法,比较两个Key的hash值来决定存放的位置
38 if hh.FHash < h.FHash then cmp := -1 else
39 if hh.FHash > h.FHash then cmp := 1 else
40 cmp := 0;
41 end else
42 begin
43 // modify by mofen //如果要进行排序,我们可以根据比较两个值的Key来决定存放的位置, 要进行排序,无可争议,必须比较Key而不能比较Key的hash值
44 cmp := CompareForSortModeString(h.Name, hh.Name);
45 end;
2 var
3 unbal, parentunbal, hh, parent: TSuperAvlEntry;
4 depth, unbaldepth: longint;
5 cmp: integer;
6 unbalbf: integer;
7 branch: TSuperAvlBitArray;
8 p: Pointer;
9 begin
10 inc(FCount);
11 h.FLt := nil;
12 h.FGt := nil;
13 h.FBf := 0;
14 branch := [];
15
16 if (FRoot = nil) then
17 FRoot := h
18 else
19 begin
20 unbal := nil;
21 parentunbal := nil;
22 depth := 0;
23 unbaldepth := 0;
24 hh := FRoot;
25 parent := nil;
26 repeat
27 if (hh.FBf <> 0) then
28 begin
29 unbal := hh;
30 parentunbal := parent;
31 unbaldepth := depth;
32 end;
33 if hh.FHash <> h.FHash then
34 begin
35 if nowSortMode = sosmDefault then
36 begin
37 //original code //这里是原始的方法,比较两个Key的hash值来决定存放的位置
38 if hh.FHash < h.FHash then cmp := -1 else
39 if hh.FHash > h.FHash then cmp := 1 else
40 cmp := 0;
41 end else
42 begin
43 // modify by mofen //如果要进行排序,我们可以根据比较两个值的Key来决定存放的位置, 要进行排序,无可争议,必须比较Key而不能比较Key的hash值
44 cmp := CompareForSortModeString(h.Name, hh.Name);
45 end;
1 function TSuperAvlTree.CompareForSortModeString(pvKey1, pvKey2: SOString):
2 Integer;
3 var
4 cmp: integer;
5 lvKey1, lvKey2: SOString;
6 begin
7 lvKey1 := LowerCase(pvKey1);
8 lvKey2 := LowerCase(pvKey2);
9 if lvKey1 <> lvKey2 then
10 begin
11 case nowSortMode of
12 sosmAdd: cmp := 1;
13 sosmASC: if lvKey2 < lvKey1 then cmp := 1 else if lvKey2 > lvKey1 then cmp := -1;
14 sosmDesc: if lvKey2 < lvKey1 then cmp := -1 else if lvKey2 > lvKey1 then cmp := 1;
15 else
16 raise Exception.Create('默认排序不采用compareForSortModeString');
17 end;
18 end else
19 cmp := 0;
20 Result := cmp;
21 end;
2 Integer;
3 var
4 cmp: integer;
5 lvKey1, lvKey2: SOString;
6 begin
7 lvKey1 := LowerCase(pvKey1);
8 lvKey2 := LowerCase(pvKey2);
9 if lvKey1 <> lvKey2 then
10 begin
11 case nowSortMode of
12 sosmAdd: cmp := 1;
13 sosmASC: if lvKey2 < lvKey1 then cmp := 1 else if lvKey2 > lvKey1 then cmp := -1;
14 sosmDesc: if lvKey2 < lvKey1 then cmp := -1 else if lvKey2 > lvKey1 then cmp := 1;
15 else
16 raise Exception.Create('默认排序不采用compareForSortModeString');
17 end;
18 end else
19 cmp := 0;
20 Result := cmp;
21 end;