Date and time Sorter
Tags: javascript, programming, beginners, react
Published: 2021-11-17 04:58:39

Sorts date and time strings(date format: DD/MM/YY, Time format HH:MM~~24 hours format)
Parameter is an object array with date, time, tags values 
I know it's not that good but I wanted a time and date sorter for my app in react native and did that(I'm just a beginner)

You can use the codes below as a test

```
let dateOBJECT = [
  { tag: 'tag1', date: '06/01/2010', time: '19:40' },
  { tag: 'tag2', date: '10/10/2017', time: '10:15' },
  { tag: 'tag3', date: '13/12/2020', time: '09:10' },
  { tag: 'tag4', date: '07/10/1900', time: '20:59' },
  { tag: 'tag5', date: '07/10/1900', time: '15:59' },
  { tag: 'tag6', date: '20/11/1999', time: '20:59' },
  { tag: 'tag7', date: '10/10/2019', time: '05:59' },
  { tag: 'tag8', date: '13/12/2020', time: '06:10' }
]

console.log(checkGreatestDateInLists(dateOBJECT))
```



Here is the code:
```
function checkGreatestDate (date1P, date2P, time1P, time2P) {
  let date1 = date1P
  let date2 = date2P

  let date1Year = date1.slice(6, 10)
  let date2Year = date2.slice(6, 10)

  let date1Month = date1.slice(3, 5)
  let date2Month = date2.slice(3, 5)

  let date1Day = date1.slice(0, 2)
  let date2Day = date2.slice(0, 2)

  date1Year = parseInt(date1Year)
  date2Year = parseInt(date2Year)

  date1Month = parseInt(date1Month)
  date2Month = parseInt(date2Month)

  date1Day = parseInt(date1Day)
  date2Day = parseInt(date2Day)

  if (date1Year > date2Year) {
    return date1
  } else if (date2Year > date1Year) {
    return date2
  } else if (date1Month > date2Month) {
    return date1
  } else if (date2Month > date1Month) {
    return date2
  } else if (date1Day > date2Day) {
    return date1
  } else if (date2Day > date1Day) {
    return date2
  } else {
    if (time1P != undefined && time2P != undefined) {
      let greatestTime = checkGreatestTime(time1P, time2P)
      if (greatestTime == time1P) {
        return date1
      } else {
        return date2
      }
    } else {
      return date1
    }
  }
}

function checkGreatestTime (time1P, time2P) {
  let time1 = time1P
  let time2 = time2P

  let time1Hours = time1P.slice(0, 2)
  let time2Hours = time2P.slice(0, 2)

  let time1Minutes = time1P.slice(3, 5)
  let time2Minutes = time2P.slice(3, 5)

  time1Hours = parseInt(time1Hours)
  time2Hours = parseInt(time2Hours)

  time1Minutes = parseInt(time1Minutes)
  time2Minutes = parseInt(time2Minutes)

  if (time1Hours > time2Hours) {
    return time1
  } else if (time2Hours > time1Hours) {
    return time2
  } else if (time1Minutes > time2Minutes) {
    return time1
  } else if (time2Minutes > time1Minutes) {
    return time2
  } else {
    return time1
  }
}

function checkGreatestDateInLists (dataListParameter) {
  let greatestIndex
  let greatestDate = '00/00/0000'
  let previousGreatestDate = '00/00/0000'
  let greatestTime = '00:00'
  let greatestTag = ''
  let sortedArr = []

  let tagArr = []
  let dateArr = []
  let timeArr = []

  for (let i = 0; i < dataListParameter.length; i++) {
    dateArr.push(dataListParameter[i].date)
    timeArr.push(dataListParameter[i].time)
    tagArr.push(dataListParameter[i].tag)
  }

  do {
    for (let i = 0; i < dateArr.length; i++) {
      previousGreatestDate = greatestDate
      greatestDate = checkGreatestDate(
        dateArr[i],
        greatestDate,
        timeArr[i],
        greatestTime
      )

      if (greatestDate != previousGreatestDate) {
        greatestTime = timeArr[i]
      } else {
        greatestTime = checkGreatestTime(greatestTime, timeArr[i])
      }
    }

    greatestIndex = dataListParameter.findIndex(
      item => item.date == greatestDate
    )
    greatestTag = tagArr[greatestIndex]

    dataListParameter.splice(greatestIndex, 1)
    dateArr.splice(greatestIndex, 1)
    greatestTime = timeArr.splice(greatestIndex, 1)[0]
    tagArr.splice(greatestIndex, 1)

    sortedArr = [
      { date: greatestDate, time: greatestTime, tag: greatestTag },
      ...sortedArr
    ]
    greatestDate = '00/00/0000'
    greatestTime = '00:00'
  } while (dataListParameter.length != 0)

  let sortedAfterTimeArr = checkGreatestTimeInLists(sortedArr)
  let sortedTimeArr = []
  let sortedTagArr = []

  for (let index = 0; index < sortedAfterTimeArr.length; index++) {
    sortedTimeArr.push(sortedAfterTimeArr[index].time)
    sortedTagArr.push(sortedAfterTimeArr[index].tag)
  }

  for (let v = 0; v < sortedArr.length; v++) {
    if (sortedTimeArr[v] != undefined) {
      sortedArr[v].time = sortedTimeArr[v]
    }
    if (sortedTagArr[v] != undefined) {
      sortedArr[v].tag = sortedTagArr[v]
    }
  }

  return sortedArr
}

function checkGreatestTimeInLists (sortedByDateArr) {
  let tagArr = []
  let timeArr = []
  let dateArr = []
  let sameArray = []
  let AlreadyCheckedARRAY = []
  for (let i = 0; i < sortedByDateArr.length; i++) {
    timeArr.push(sortedByDateArr[i].time)
    dateArr.push(sortedByDateArr[i].date)
    tagArr.push(sortedByDateArr[i].tag)
  }

  //looping taking the value
  for (let i = 0; i < dateArr.length; i++) {
    const date = dateArr[i]

    let alreadyChecked = false

    for (let i = 0; i < AlreadyCheckedARRAY.length; i++) {
      const verifyingDate = AlreadyCheckedARRAY[i]
      if (verifyingDate == date) {
        alreadyChecked = true
      }
    }

    if (alreadyChecked == false) {
      AlreadyCheckedARRAY.push(date)
      let amountOftimes = 0

      for (let k = 0; k < dateArr.length; k++) {
        const secondDate = dateArr[k]

        if (secondDate == date && amountOftimes >= 1) {
          amountOftimes = amountOftimes + 1
        } else if (secondDate == date) {
          amountOftimes = amountOftimes + 1
        }
      }
      sameArray.push({ date: date, repeating: amountOftimes })
    }
  }

  let repeatInArray = []
  let maxIndex = 0

  for (let i = 0; i < sameArray.length; i++) {
    const sameArrayObject = sameArray[i]
    const numberOfRepeats = sameArrayObject.repeating
    maxIndex = maxIndex + numberOfRepeats
    maxIndex = maxIndex

    if (numberOfRepeats > 1) {
      repeatInArray.push({
        date: sameArrayObject.date,
        firstIndex: maxIndex - numberOfRepeats,
        repeating: numberOfRepeats
      })
    }
  }

  let fullySortedFinal = []
  for (let i = 0; i < repeatInArray.length; i++) {
    const repeatInArrayHere = repeatInArray[i]

    const firstIndexHere = repeatInArrayHere.firstIndex
    const numberOfRepeating = repeatInArrayHere.repeating
    const sortingTimeArr = []
    let toSortTagArr = []

    for (let i = firstIndexHere; i < firstIndexHere + numberOfRepeating; i++) {
      sortingTimeArr.push(timeArr[i])
      toSortTagArr.push(tagArr[i])
    }

    let finallySortedArr = []
    let finallySortedTag = []
    do {
      let greatestTime = '00:00'

      for (let i = 0; i < sortingTimeArr.length; i++) {
        let thisTime = sortingTimeArr[i]

        greatestTime = checkGreatestTime(greatestTime, thisTime)
      }

      finallySortedArr = [greatestTime, ...finallySortedArr]

      let greatestIndex = sortingTimeArr.findIndex(item => item == greatestTime)
      finallySortedTag = [toSortTagArr[greatestIndex], ...finallySortedTag]

      sortingTimeArr.splice(greatestIndex, 1)
      toSortTagArr.splice(greatestIndex, 1)
      greatestTime = '00:00'
    } while (sortingTimeArr.length != 0)

    for (let i2 = 0; i2 < finallySortedArr.length; i2++) {
      timeArr[firstIndexHere + i2] = finallySortedArr[i2]
      tagArr[firstIndexHere + i2] = finallySortedTag[i2]
    }

    for (b = 0; b < timeArr.length; b++) {
      fullySortedFinal.push({ time: timeArr[b], tag: tagArr[b] })
    }
  }

  return fullySortedFinal
}
```