Home > Enterprise >  sort is not returning the correctly ordered values
sort is not returning the correctly ordered values

Time:10-16

I am attempting to sort an array of objects by specific values, based on what the user has selected. The function is returning a result, but its always the same, regardless of which case runs. I have tested that each case is running correctly and they are, but the sort function doesn't seem to work.

I am building a nextjs application and running the sort within useEffect each time the sortBy value is changed.

const x = [
    {
      timestamp: 1000,
      fairity: 1,
      communication: 3,
      manners: 3,
      location: 2,
      price: 5,
      overall: 4
    },
    {
      timestamp: 234,
      fairity: 4,
      communication: 2,
      manners: 4,
      location: 1,
      price: 1,
      overall: 1
    },
    {
      timestamp: 23432,
      fairity: 4,
      communication: 1,
      manners: 2,
      location: 1,
      price: 4,
      overall: 3
    },
  ]

  const sortByOptions = {
    DATE_ASC: 'DATE_ASC',
    DATE_DESC: 'DATE_DESC',
    LANDLORD_ASC: 'LANDLORD_ASC',
    LANDLORD_DESC: 'LANDLORD_DESC',
    PROPERTY_ASC: 'PROPERTY_ASC',
    PROPERTY_DESC: 'PROPERTY_DESC'
  };

  const sortReviews = (_reviews, by) => {
    console.log('asc', _reviews.sort((a, b) => a.timestamp - b.timestamp))
    console.log('desc', _reviews.sort((a, b) => b.timestamp - a.timestamp))
    switch (by) {
      case sortByOptions.DATE_ASC:
        return _reviews.sort((a, b) => a.timestamp - b.timestamp);
      case sortByOptions.DATE_DESC:
        return _reviews.sort((a, b) => b.timestamp - a.timestamp);
      case sortByOptions.LANDLORD_ASC:
        return _reviews.sort((a, b) => (a.manners   a.fairity   a.communication) - (b.manners   b.fairity   b.communication));
      case sortByOptions.LANDLORD_DESC:
        return _reviews.sort((a, b) => (b.manners   b.fairity   b.communication) - (a.manners   a.fairity   a.communication));
      case sortByOptions.PROPERTY_ASC:
        return _reviews.sort((a, b) => (a.location   a.price   a.overall) - (b.location   b.price   b.overall));
      case sortByOptions.PROPERTY_DESC:
        return _reviews.sort((a, b) => (b.location   b.price   b.overall) - (a.location   a.price   a.overall));
      default:
        return [];
    }
  };

  console.log(sortReviews(x, sortByOptions.DATE_DESC))

CodePudding user response:

I realized that I was using the sort function incorrectly. I thought it returned a value, but instead it simply mutates the given array. I modified my implementation to simply do this when the map occurs.

const sortByOptions = {
    DATE_ASC: 'DATE_ASC',
    DATE_DESC: 'DATE_DESC',
    LANDLORD_ASC: 'LANDLORD_ASC',
    LANDLORD_DESC: 'LANDLORD_DESC',
    PROPERTY_ASC: 'PROPERTY_ASC',
    PROPERTY_DESC: 'PROPERTY_DESC'
  };

  const sortByFunctions = {
    DATE_ASC: (a, b) => a.timestamp - b.timestamp,
    DATE_DESC: (a, b) => b.timestamp - a.timestamp,
    LANDLORD_ASC: (a, b) => (a.manners   a.fairity   a.communication) - (b.manners   b.fairity   b.communication),
    LANDLORD_DESC: (a, b) => (b.manners   b.fairity   b.communication) - (a.manners   a.fairity   a.communication),
    PROPERTY_ASC: (a, b) => (a.location   a.price   a.overall) - (b.location   b.price   b.overall),
    PROPERTY_DESC: (a, b) => (b.location   b.price   b.overall) - (a.location   a.price   a.overall)
  };


reviews.sort(sortByFunctions[sortBy]).map((x, i) => (...));
  • Related