Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何在React with Typescript上对组合框项目进行排序?_Reactjs_Typescript_React Native - Fatal编程技术网

Reactjs 如何在React with Typescript上对组合框项目进行排序?

Reactjs 如何在React with Typescript上对组合框项目进行排序?,reactjs,typescript,react-native,Reactjs,Typescript,React Native,我有一个项目,项目中有一个年度组合框。我将把这一年分类为asc到desc或desc到asc 我有一个表单utils,我在组件中调用utils。试试这种方法 import React, { Component } from "react"; import Select from "react-select"; function compare(a, b) { return a.label > b.label ? 1 : b.label >

我有一个项目,项目中有一个年度组合框。我将把这一年分类为asc到desc或desc到asc

我有一个表单utils,我在组件中调用utils。

试试这种方法

import React, { Component } from "react";
import Select from "react-select";

function compare(a, b) {
  return a.label > b.label ? 1 : b.label > a.label ? -1 : 0;
}

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      options: [
        {
          label: 1940,
          value: 1
        },
        {
          label: 1945,
          value: 2
        },
        {
          label: 1942,
          value: 3
        },
        {
          label: 1941,
          value: 4
        },
        {
          label: 1946,
          value: 5
        }
      ]
    };
  }

  render() {
    return (
      <Select
        //isMulti
        hideSelectedOptions={false}
        options={this.state.options.sort(compare)}
      />
    );
  }
}

这里有一个

除非你提供足够的问题信息,否则没有人能帮助你。至少您可以包含您正在使用的代码。