-1
list = ["Conversation With Bob May 10, 2017 13:05", 
        "Conversation With Bob May 10, 2017 9:22", 
        "Conversation With Alice May 12, 2017 4:12",
        "Conversation With Alice May 8, 2017 3:59",
        "Conversation With Kevin December 12, 2017 7:55",
        "Conversation With Tom January 5, 2017 16:00",
        "Conversation With Alice, Bob, Kevin February 5, 2017 21:00",
        "Conversation With Alice, Kevin March 12, 2017 9:45"]

I want to sort this list first alphabetically by the "Conversation With Alice" part, and then by date/time.

I know I have to do something along the lines of :

list.sort(
    function(a,b) {
        //DO SOMETHING
    }
)

But I'm having trouble getting this sort correct.

EDIT: Sorry I meant in javascript.

symmetry96
  • 13
  • 3
  • 5
    You should use a tuple of strings, not a single string. You'll probably just end up creating such a tuple from the string. – juanpa.arrivillaga May 18 '17 at 23:06
  • 1
    Also, convert the date to a DateTime object; those sort nicely within themselves. – Prune May 18 '17 at 23:08
  • Well, then there's a bit more work, I'd suggest extract time from string to some datetime object, and sort it by 2 fields. – Oleksandr Verhun May 18 '17 at 23:08
  • yes, you really do need to `//DO SOMETHING` :p – Jaromanda X May 18 '17 at 23:16
  • 1
    Please show us what your attempts were that you couldn't get correct. – Bergi May 18 '17 at 23:17
  • @Jaromanda X I meant the format of the code. Do you really have to be condescending? I already have a working version. I'm just wondering if there is a more clean way to do it. – symmetry96 May 18 '17 at 23:24
  • Possible duplicate of [Sort array by firstname (alphabetically) in Javascript](http://stackoverflow.com/questions/6712034/sort-array-by-firstname-alphabetically-in-javascript) – zero298 May 18 '17 at 23:33
  • `I already have a working version` - really? I don't think `//DO SOMETHING` actually does what you are asking. How about you post what you have working - and I wasn't being condescending, I was pointing out the fact that SO isn't a code writing service :p – Jaromanda X May 18 '17 at 23:36
  • Should "Åsa" come before or after "Håkan"? Should it depend on each user's culture? – Tom Blodget May 18 '17 at 23:56

2 Answers2

1

For the conversion of date string to a Date object I would recommend you find a library that does that for you, why re-invent the wheel - in the following, I'm using momentjs

var list = [
    "Conversation With Bob May 10, 2017 13:05",
    "Conversation With Bob May 10, 2017 9:22",
    "Conversation With Alice May 12, 2017 4:12",
    "Conversation With Alice May 8, 2017 3:59",
    "Conversation With Kevin December 12, 2017 7:55",
    "Conversation With Tom January 5, 2017 16:00",
    "Conversation With Alice, Bob, Kevin February 5, 2017 21:00",
    "Conversation With Alice, Kevin March 12, 2017 9:45"
];

var sorted = list.map(item => {
    let s = item.split(' '),
        d = s.splice(-4),
        date = moment(d.join(' '), 'MMMM do, YYYY h:mm').toDate(),
        text = s.join(' ');

    return { item, text, date };
})
.sort((a, b) => a.text.localeCompare(b.text) || (a.date - b.date))
.map(item => item.item);

console.log(sorted.join('\n'));
<script src="https://momentjs.com/downloads/moment.min.js"></script>
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
0

If you want to sort this list by name you can simply find the index of the second space and then add 1 to that position to find the first char of the name. After that you can sort the string by using the ASCII value for that char. Otherwise, if you want to sort by date, you need to find the date and transform it in to a date object. Then you can simply control the timestamp of the date to sort the strings.

PazzoTotale
  • 411
  • 2
  • 5
  • 14
  • A "char" doesn't have an ASCII value because it is a UTF-16 code unit, one or two of which encode a Unicode codepoint. One sort would be lexicographically by code units. Another would be lexicographically by codepoints. Others would by a specific culture. When a user wants an alphabetical sort, they probably think in terms of the language and culture of either where they are from, where they are, or where their company's home office is.Of course, what's wanted is not clear from the question. – Tom Blodget May 18 '17 at 23:45
  • @Tom Blodget thanks for the explanation, yes of course he doesn't explain quite well the context – PazzoTotale May 19 '17 at 05:47