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
|
To: vim_dev@googlegroups.com
Subject: Patch 7.4.341
Fcc: outbox
From: Bram Moolenaar <Bram@moolenaar.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
------------
Patch 7.4.341
Problem: sort() doesn't handle numbers well.
Solution: Add an argument to specify sorting on numbers. (Christian Brabandt)
Files: runtime/doc/eval.txt, src/eval.c, src/testdir/test55.in,
src/testdir/test55.ok
*** ../vim-7.4.340/runtime/doc/eval.txt 2014-06-25 14:39:35.094348583 +0200
--- runtime/doc/eval.txt 2014-06-25 17:05:50.606680574 +0200
***************
*** 5618,5628 ****
If you want a list to remain unmodified make a copy first: >
:let sortedlist = sort(copy(mylist))
- < Uses the string representation of each item to sort on.
- Numbers sort after Strings, |Lists| after Numbers.
- For sorting text in the current buffer use |:sort|.
! When {func} is given and it is one then case is ignored.
When {func} is a |Funcref| or a function name, this function
is called to compare items. The function is invoked with two
items as argument and must return zero if they are equal, 1 or
--- 5628,5647 ----
If you want a list to remain unmodified make a copy first: >
:let sortedlist = sort(copy(mylist))
! < When {func} is omitted, is empty or zero, then sort() uses the
! string representation of each item to sort on. Numbers sort
! after Strings, |Lists| after Numbers. For sorting text in the
! current buffer use |:sort|.
!
! When {func} is given and it is is '1' or 'i' then case is
! ignored.
!
! When {func} is given and it is 'n' then all items will be
! sorted numerical (Implementation detail: This uses the
! strtod() function to parse numbers, Strings, Lists, Dicts and
! Funcrefs will be considered as being 0).
!
When {func} is a |Funcref| or a function name, this function
is called to compare items. The function is invoked with two
items as argument and must return zero if they are equal, 1 or
*** ../vim-7.4.340/src/eval.c 2014-06-17 17:48:21.776628008 +0200
--- src/eval.c 2014-06-25 17:23:05.466719724 +0200
***************
*** 17330,17335 ****
--- 17330,17336 ----
item_compare2 __ARGS((const void *s1, const void *s2));
static int item_compare_ic;
+ static int item_compare_numeric;
static char_u *item_compare_func;
static dict_T *item_compare_selfdict;
static int item_compare_func_err;
***************
*** 17359,17368 ****
p1 = (char_u *)"";
if (p2 == NULL)
p2 = (char_u *)"";
! if (item_compare_ic)
! res = STRICMP(p1, p2);
else
! res = STRCMP(p1, p2);
vim_free(tofree1);
vim_free(tofree2);
return res;
--- 17360,17379 ----
p1 = (char_u *)"";
if (p2 == NULL)
p2 = (char_u *)"";
! if (!item_compare_numeric)
! {
! if (item_compare_ic)
! res = STRICMP(p1, p2);
! else
! res = STRCMP(p1, p2);
! }
else
! {
! double n1, n2;
! n1 = strtod((char *)p1, (char **)&p1);
! n2 = strtod((char *)p2, (char **)&p2);
! res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
! }
vim_free(tofree1);
vim_free(tofree2);
return res;
***************
*** 17439,17444 ****
--- 17450,17456 ----
return; /* short list sorts pretty quickly */
item_compare_ic = FALSE;
+ item_compare_numeric = FALSE;
item_compare_func = NULL;
item_compare_selfdict = NULL;
if (argvars[1].v_type != VAR_UNKNOWN)
***************
*** 17457,17462 ****
--- 17469,17487 ----
item_compare_ic = TRUE;
else
item_compare_func = get_tv_string(&argvars[1]);
+ if (item_compare_func != NULL)
+ {
+ if (STRCMP(item_compare_func, "n") == 0)
+ {
+ item_compare_func = NULL;
+ item_compare_numeric = TRUE;
+ }
+ else if (STRCMP(item_compare_func, "i") == 0)
+ {
+ item_compare_func = NULL;
+ item_compare_ic = TRUE;
+ }
+ }
}
if (argvars[2].v_type != VAR_UNKNOWN)
*** ../vim-7.4.340/src/testdir/test55.in 2014-03-25 18:23:27.062087691 +0100
--- src/testdir/test55.in 2014-06-25 17:20:47.006714486 +0200
***************
*** 332,337 ****
--- 332,342 ----
:$put =string(reverse(sort(l)))
:$put =string(sort(reverse(sort(l))))
:$put =string(uniq(sort(l)))
+ :let l=[7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'foo', 'FOOBAR',{}, []]
+ :$put =string(sort(copy(l), 'n'))
+ :$put =string(sort(copy(l), 1))
+ :$put =string(sort(copy(l), 'i'))
+ :$put =string(sort(copy(l)))
:"
:" splitting a string to a List
:$put =string(split(' aa bb '))
*** ../vim-7.4.340/src/testdir/test55.ok 2014-03-25 18:23:27.062087691 +0100
--- src/testdir/test55.ok 2014-06-25 17:23:31.382720704 +0200
***************
*** 101,106 ****
--- 101,110 ----
[[0, 1, 2], [0, 1, 2], 4, 2, 2, 1.5, 'xaaa', 'x8', 'foo6', 'foo', 'foo', 'A11', '-0']
['-0', 'A11', 'foo', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 2, 4, [0, 1, 2], [0, 1, 2]]
['-0', 'A11', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 4, [0, 1, 2]]
+ [-1, 0, 0, 'foo', 'FOOBAR', {}, [], 1.0e-15, 0.22, 7, 9, 12, 18, 22, 255]
+ ['foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}]
+ ['foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}]
+ ['FOOBAR', 'foo', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}]
['aa', 'bb']
['aa', 'bb']
['', 'aa', 'bb', '']
*** ../vim-7.4.340/src/version.c 2014-06-25 15:02:29.250400570 +0200
--- src/version.c 2014-06-25 16:46:45.438637250 +0200
***************
*** 736,737 ****
--- 736,739 ----
{ /* Add new patch number below this line */
+ /**/
+ 341,
/**/
--
We do not stumble over mountains, but over molehills.
Confucius
/// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
|