简体   繁体   English

如何按字母顺序对下拉菜单项排序?

[英]How can I sort drop-down menu items alphabetically?

Is there any way to display options in an alphabetical order in the drop-down menu of Material-UI ? Material-UI的下拉菜单中,是否可以按字母顺序显示options

I know that arrays can be sorted simply using arr.sort() . 我知道可以使用arr.sort()对数组进行排序。 However, if I do const options = [...].sort() , then I still see unsorted values in the drop-down menu. 但是,如果我执行const options = [...].sort() ,那么我仍然会在下拉菜单中看到未排序的值。

const options = [
  {label:"B",value:8033.86},
  {label:"A",value:483.93},
  {label:"Z",value:1246.3},
  {label:"C",value:145.0},
  {label:"E",value:244.5}
]

<Grid item xs={true}>
  <FormControl
      className={this.props.styles.formControl}
      margin="normal">
      <InputLabel shrink htmlFor="distanceTarget-label-placeholder">
          Target:
      </InputLabel>
      <Select
        onChange={(event) => this.props.handleChange("distanceTarget", event)}
        value={this.props.state.distanceTarget}
        input={<Input name="distanceTarget" id="distanceTarget-label-placeholder" />}
        displayEmpty="true"
        name="distanceTarget"
      >
      {options && options.length && options.map((option, i) => {
          return <MenuItem value={option.value} key={i}>{option.label}</MenuItem>
      })}
      </Select>
  </FormControl>
</Grid>

here a simple way to sort the array of objects and you can read the documentation 这是对对象数组进行排序的一种简单方法,您可以阅读文档

 const options = [ {label:"B",value:8033.86}, {label:"A",value:483.93}, {label:"Z",value:1246.3}, {label:"C",value:145.0}, {label:"E",value:244.5} ] console.log(options.sort((a, b) => (a.label > b.label) ? 1 : -1)) 

Material-UI is about styles, not logic. Material-UI与样式有关,而不是逻辑。 First of all, you need to sort your options. 首先,您需要对选项进行排序。

    for (const item in options) {
     console.log(options[item].label)
    }

Its first steps. 它的第一步。 The second you should try yourself. 第二,你应该尝试一下。

When you're dealing with an Array of Objects, you need to tell what determines which one comes first: 在处理对象数组时,您需要告诉确定哪个决定首先出现:

function sortAlphabeticallyAscending(a,b){
    if(a.label < b.label) return -1;
    else if (a.label > b.label) return 1;
    else return 0;
}

options.sort(sortAlphabeticallyAscending);

basically, return -1 if you want to say that the index of a is lower than b (ie a comes first), return 1 if you want to say that b comes first, and 0 if you don't want to change the order 基本上,如果要说a的索引小于b(即a排在前),则返回-1;如果要说b排在第一位,则返回1;如果不想更改顺序,则返回0。

See the section compareFunction in the docs... 请参阅文档中的compareFunction部分...

You have to define the sorting logic as your input array is an array of objects. 您必须定义排序逻辑,因为您的输入数组是对象数组。

More info can be found here 更多信息可以在这里找到

 const options = [ {label:"B",value:8033.86}, {label:"A",value:483.93}, {label:"Z",value:1246.3}, {label:"C",value:145.0}, {label:"E",value:244.5} ]; const objComparator = function(a, b) { if (a.label < b.label) { return -1; } if (a.label > b.label) { return 1; } return 0; }; options.sort(objComparator); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM