Sort method of my flatList

Asked

Viewed 195 times

0

 <FlatList
                style={{backgroundColor: 'white', marginTop: 10}}
                data={this.state.progr.data.sort((a, b) => a.start_time > b.start_time).filter(x => x.period === '2019-10-17T00:00:00.000-03:00')}
                renderItem={({item}) => this.renderItem({item})}
                keyExtractor = {(item, index) => item.id.toString()}
            />

The start_time being ordered gives me a JSON date, so 2019-09-10T14:00:00.000-03:00". I want to order by the hour.

I’ve tried it this way:

<FlatList
                style={{backgroundColor: 'white', marginTop: 10}}
                data={this.state.progr.data.sort((a, b) => a.(new Date(start_time).getHours()) > b.(new Date(start_time).getHours()).filter(x => x.period === '2019-10-17T00:00:00.000-03:00')}
                renderItem={({item}) => this.renderItem({item})}
                keyExtractor = {(item, index) => item.id.toString()}
            />

I tried it like this and it didn’t crt. Can someone help me with the syntax please?!

1 answer

1


To sort by the time use this code similar to the one I sent you before by date:

let listaRecebida = [
	{"period": "2019-10-19T19:00:00.000-03:00"},
	{"period": "2019-10-11T11:00:00.000-03:00"},
	{"period": "2019-10-16T16:00:00.000-03:00"},
	{"period": "2019-10-13T13:00:00.000-03:00"},
	{"period": "2019-10-27T07:00:00.000-03:00"},
	{"period": "2019-10-19T19:03:00.000-03:00"},
	{"period": "2019-10-19T19:01:00.000-03:00"},
	{"period": "2019-10-19T19:02:00.000-03:00"},
];

function apenasHoras(dataStr) {
  const data = new Date(dataStr);
  const horas = data.getHours().toString().padStart(2, '0');
  const minutos = data.getMinutes().toString().padStart(2, '0');
  const segundos = data.getSeconds().toString().padStart(2, '0');
  return `${horas}${minutos}${segundos}`;
}

listaOrdenada = listaRecebida.sort(
	function(a, b){
  	const varA = apenasHoras(a.period);
  	const varB = apenasHoras(b.period);
  	return varA-varB;
  }
)

console.log('Lista ordenada');
listaOrdenada.forEach(item => {
  console.log(item.period);
});

I want to give you 2 observations/indications:

  1. Sort when receiving the object and not in rendering it.

  2. No filter is used to sort. Filter as the name says is to filter, that is, only items that return positive in the filter routine will be displayed.

Any doubt we’re here.

  • The filter is not being used to sort, it is being used to filter msm. Because JSON (q n was created by me) has one item for the date, one item for the initial hr, and one item for the final hr

  • If you notice in the above code, Sort is used with start_time and filter with period. They are distinct

  • I’m ordering the rendering, because the sheet has 4 flatList... all rendered with a different filter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.