Custom Sorting for String Values in Price Column to Improve Data Relevance

I have a column in our application that primarily displays numeric values for prices. Occasionally, this column needs to show text (e.g., "Inquire for price") when a numeric value is unavailable, typically when a vendor requires direct inquiries. Currently, I convert the entire column to a string type to accommodate text entries alongside numeric values.

Issue: The challenge arises with the default sorting behavior of this mixed-data column. Text entries ("Inquire for price") automatically sort to the top of the column, reducing the visibility of more relevant numeric data.

Objective: I seek a method to assign a measurable value to these text entries so they can be sorted as if they were numeric. Ideally, these text entries should appear towards the bottom of the column in sorted views, allowing numeric values to remain prominently at the top.

Proposed Solution: Is it possible to implement a sorting logic where text entries are assigned a nominal high numeric value during sorting operations? This adjustment would ensure that numeric prices are prioritized in the display order.

Please see the attached screenshot for reference.

Expected Outcome: With the proposed change, users would see the most relevant numeric price data first, with text entries sorted to the bottom, enhancing data usability and relevance.

  • Insertion Sort
  • Bubble Sort
  • Merge Sort
  • Selection Sort
  • Quick Sort

these are the 5 basic sorting algorithms (there are lots more, but these will be the goto for 90% of situations). So, which to choose.... there are 2 main factors to consider. How likely data will naturally be somewhat sorted/how much of the data is already sorted and how much data needs to be sorted (count/array length) with the later having a bigger impact. If you're not too worried about any of that Insertion Sort or Quick Sort will probably be the preferred ones. Insertion Sort is fast and is best for small data sets or when the data is likely to be nearly already sorted. Quick Sort is also fast for small data sets, but with this one if 2 items have the same value the order isn't guaranteed to be preserved. so if list item #2 has a value of 55 and list item #8 also has a value of 55, after sorting you could have #8 appear before #2 (which is the opposite of what they were before sorting). If you don't care what order items are in if they have the same value, go with Quick Sort. Bellow is a quicksort function you can use:

const quickSort = (myArray) => {
  if (myArray.length <= 1) {
    return arr;
  }

  // this can be any of the array items, I picked the middle as it's simple.  
  // since we're dealing with mixed types though, we do need to check it's value and have a backup index/pivot in case of a string.
  // generally you want to avoid using the first or last index as the pivot as it increases the odds of hitting a worse-case scenario of O(n^2).  the majority of the time you'll get around O(n log(n))
  let mid = myArray.length / 2;
  let pivot = myArray[typeof myArray[mid ]  !== 'string'? mid : 0];

  let leftArray = [];
  let rightArray = [];

  for (let i = 1; i < myArray.length; i++) {
    // automatically add string values to the end of the sorted list
    if (typeof myArray[i] === 'string'){
      rightArray .push(myArray[i]);
    }
    // sort numeric values
    else{
      if (myArray[i] < pivot) {
        leftArray .push(myArray[i]);
      } else {
        rightArray .push(myArray[i]);
      }
    }
  }

  return [...quickSort(leftArray ), pivot, ...quickSort(rightArray )];
};

it's normally suggested to use the median-of-3 (median of the first, last and mid values) but this becomes more difficult when you could pick a string value and accidently try to calculate the median. you'd get a value for it, there's just REALLY good odds of it ending up being the last index, which can result in worst-case scenarios occuring way more often than they should slowing everything down. if you notice the sort getting super slow with larger arrays you can try using different values for the pivot before implementing a new sorting algo

for more sort algo implementations w JS code (and some really nifty optimizations) you can go here

edit: forgot to mention, since this doesn't reference any values not passed in as a parameter you can make this a function and reuse it wherever by simply using {{ myQuickSort(variable1.value) }}. you could probably also use a transformer, but I'm not 100% sure since I don't normally use them
image