M
M
Michael2017-02-20 01:18:17
go
Michael, 2017-02-20 01:18:17

Why can't the array be sorted?

There is an array of type Item:

type Item struct {
  Title       string `xml:"title"`
  Link        string `xml:"link"`
  Description string `xml:"description"`
  Date        string `xml:"pubDate"`
}

The Date field is stored like this: "Sat, 18 Feb 2017 16:40:05 +0300"
I'm trying to sort records by date like this:
func sortNews(feeds []Item) []Item {
  length := len(feeds)
  for i := 0; i < length-1; i++ {
    oneDate, _ := time.Parse(time.RFC1123Z, feeds[i].Date)
    for j := i+1; j < length; j++ {
      nextDate, _ := time.Parse(time.RFC1123Z, feeds[j].Date)
      b := nextDate.Before(oneDate)
      if b {
        trash := feeds[i].Date
        feeds[i].Date = feeds[j].Date
        feeds[j].Date = trash
      }
    }
  }

  return feeds
}

I checked. time.Parse works fine and correctly, but for some reason the array is not sorted

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question