如何以与基于网络的楼梯生成器相同的方式对文本值进行排序

问题描述 投票:0回答:2

我正在尝试使用与此基于网络的楼梯生成器相同的方式对一些文本值进行排序(https://authoredup.com/tools/text-staircase)。

enter image description here

这是我的价值观

A2:A21

Bhutan
The United Kingdom of Great Britain and Northern Ireland
South Africa
The Republic of the Marshall Islands
Netherlands
Central African Republic
Dominica
The Democratic Republic of the Congo
Peru
Papua New Guinea
Jamaica
Antigua and Barbuda
El Salvador
Bosnia and Herzegovina
Japan
The Democratic Republic of São Tomé and Príncipe
Nicaragua
The Federated States of Micronesia
Bangladesh
The Independent and Sovereign Republic of Kiribati

有四种变体,我已经成功完成了两种更简单的变体。对于短到长,

B2
中的公式是
=SORTBY(A2:A21,LEN(A2:A21))
,对于
C2
中的长到短,是
=SORTBY(A2:A21,LEN(A2:A21),-1)

但是,我正在努力想出金字塔和 V 形样式的公式(我已在屏幕截图中包含了值以显示应返回的内容)。

我能做什么?

excel excel-formula
2个回答
0
投票

对列表进行两次排序。 第一次,按长度排序,就像您已经在做的那样。

第二次,按照其在长度排序列表中的位置的函数进行排序(这里可以使用

SEQUENCE
函数),例如
-2^[Position]

位置 -2^[位置] 已排序位置 已排序-2^[位置]
1 -2 5 -32
2 4 3 -8
3 -8 1 -2
4 16 2 4
5 -32 4 16

这会将第一个项目从第一个排序列表移动到第二个排序列表的中间,并交替其之前或之后的后续项目。

=SORTBY(SORTBY(A2:A21,LEN(A2:A21), 1), -2^SEQUENCE(COUNTA(A2:A21)), 1)

要在“Pyramid”和“Chevron”之间交换,请交换first排序的顺序 - 这将确定中间值是最短还是最长。


为了稍微简化一些事情,您还可以使用

LET
函数 为每个函数定义一次范围:

#Short to Long
=LET(_list, A2:A21, SORTBY(_list, LEN(_list), 1))

#Long to Short
=LET(_list, A2:A21, SORTBY(_list, LEN(_list), -1))

#Pyramid
=LET(_list, A2:A21, SORTBY(SORTBY(_list, LEN(_list), -1),-2^SEQUENCE(COUNTA(_list)),1))

#Chevron
=LET(_list, A2:A21, SORTBY(SORTBY(_list, LEN(_list), 1),-2^SEQUENCE(COUNTA(_list)),1))

0
投票

您可以使用这些公式:

=LET(data,A1:A20,
sortData,SORTBY(data,LEN(data)),
upperPart,INDEX(sortData,SEQUENCE(ROWS(sortData)/2,,1,2)),
lowerPart,INDEX(sortData,SEQUENCE(ROWS(sortData)/2,,2,2)),
VSTACK(SORTBY(upperPart,LEN(upperPart),-1),lowerPart))

=LET(data,A1:A20,
sortData,SORTBY(data,LEN(data)),
upperPart,INDEX(sortData,SEQUENCE(ROWS(sortData)/2,,1,2)),
lowerPart,INDEX(sortData,SEQUENCE(ROWS(sortData)/2,,2,2)),
VSTACK(upperPart,SORTBY(lowerPart,LEN(lowerPart),-1)))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.