SORTROWS

=SORTROWS(array,zerosToEnd)

array
the 2D array to be sorted
zerosToEnd
0 or blank to sort zeros normally, 1 to sort zeros to end of range

SORTROWS sorts a 2D array by row.

RicoS

Board Regular
Joined
May 1, 2019
Messages
62
SORTROWS Sorts a 2D array by row. Optionally, zeros can be classed as "high" (as in sorted last). The array returns in the same dimensions as the original.

This function utilises other Lambda functions that can be viewed here:
ARRAY2DTO1D
ROWNUMBERS
ARRAY1DTO2D

Excel Formula:
=LAMBDA(array,zerosToEnd,
   LET(a,array,
      mx,MAX(a)+1,
      a1D,ARRAY2DTO1D(a),
      a1DRw,ARRAY2DTO1D(ROWNUMBERS(a)),
      zte,IF(zerosToEnd=1,IF(a1D=0,mx,a1D),a1D),
      srt,SORTBY(a1D,a1DRw,1,zte,1),
      return,ARRAY1DTO2D(srt,ROWS(a)),
   return)
)

Lambda - Last Cell and Split.xlsx
BCDEFGHIJKL
1Original Array
210345608010
312305070010
41204567800
5
6Sorted Array, zeros at end
713456810000
812357100000
91245678000
10
11Sorted Array, zeros in place
1200013456810
1300001235710
140001245678
15
Sheet1
Cell Formulas
RangeFormula
B2:K4B2={1,0,3,4,5,6,0,8,0,10;1,2,3,0,5,0,7,0,0,10;1,2,0,4,5,6,7,8,0,0}
B7:K9B7=SORTROWS(B2#,1)
B12:K14B12=SORTROWS(B2#,)
Dynamic array formulas.
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,216,045
Messages
6,128,484
Members
449,455
Latest member
jesski

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top