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