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