Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

jQuery sort emails

I created a jQuery table that displays Users within my database. Fields displayed are First Name, Last Name, Last Login Date and Email Address.

First, Last and Login all sort correctly, however, when I try to sort the Email Address column alphabetically it doesn't because of the special character (@) being used. Any ideas how to correct this?

Hi Justin,

Are you using a sorting plugin for this? If so, which one?

How do you want emails sorted? Based on the address up to the '@'?

Hey Jason Anello I'd like to sort up to the '@' symbol. Not using a plugin.

2 Answers

I don't know how you're doing the sorting so I can't give a more customized solution but I can try to help with how to extract the first part of the email which you can then sort on that.

If you have the email as a string then you could split it on the @ and then retrieve the first element of that array which would be at index 0.

email = 'noname@example.com';
firstPart = email.split('@')[0];
console.log(firstPart); // logs 'noname'

Does that help you at all? Or did you have that part figured out but not sure how to integrate with what you already have?

If you need further help on how to integrate that with what you already have then please give more details on how you're doing the sorting and how you're storing all the table data.

This worked to a degree, had to modify the script to accommodate a few other aspects of our table.

Sorry Justin, I'm not that advanced yet. Hope you get it figured out!

All good Caleb Kleveter thanks for taking a look.