0

What is the way to sort alphabetically the "reason.Name" in picker in my example ? if i use the lodash so how should i do that ?

import { Picker } from '@react-native-picker/picker';
import _, { filter } from 'lodash';

<Picker
              mode="dropdown"
              selectedValue={selectedValue3}
              style={{
                placeholderTextColor: 'black',
                transform: [{ scaleY: 1.2 }, { scaleX: 1.2 }],
              }}
              onValueChange={(itemValue, itemIndex) => {
                dispatch(setCurrentTabInfoAction({ reason: foundReason?.Name }))
              }}
            >
              {listReasons3.map((reason, i) => (
                <Picker.Item label={reason.Name} value={reason.Code} key={i} />
              ))}
            </Picker>
Sara22
  • 27
  • 4

1 Answers1

0

you have to use Array.prototype.sort()

It accepts a comparator function as an argument -

{listReasons3
  .sort((a, b) => {
    if (a.Name === b.Name) return  0;
    if (a.Name > b.Name)   return  1;
    if (a.Name < b.Name)   return -1;
  })
  .map((reason, i) => {
    return <Picker.Item label={reason.Name} value={reason.Code} key={'picker' + i} />
   })
}

(btw keys should be strings)

edit: with lodash -

_.sortBy(listReasons3, ['Name']).map(...);
LonelyCpp
  • 2,533
  • 1
  • 17
  • 36