9 Replies - 250 Views - Last Post: 11 February 2019 - 01:55 PM Rate Topic: -----

#1 bluemarmalade   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 05-July 15

How to sort emails in a string?

Posted 11 February 2019 - 05:00 AM

I have a string with email adresses separated either with a comma or a new line.
Now I have a method to sort them into a list of valid email adresses. But I need to also check if an emailadress is not listed more than once. But how to do this in a good way? This is a webapp using asp.net core.

this returns a list of emails:
public static List<string> SortToListOfEmailstrings(string emailstring)
        {
            var emails = new List<string>();
            if (string.IsNullOrWhiteSpace(emailstring))
            {
                return emails;
            }
            string[] separatingChars = { "\r\n", "," };
            emails = emailstring.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();
            //checks if each string are valid emails using the build in dataannotations class EmailAddressAttribute()
            var emailCheck = new EmailAddressAttribute();
            var newEmails = new List<string>();
            foreach (var item in emails)
            {
                //i could just check if not valid and remove the item instead of creating a new list. 
                if (emailCheck.IsValid(item))
                {
                    newEmails.Add(item);
                }
            }
            return newEmails;
        }


            //would this solve my problem?:var newEmails = new HashSet<string>()   then return newEmails.ToList()



I'm thinking this can be done using a hashtable, where you can only have one of each item, but i don't know how to do it correctly.

This post has been edited by bluemarmalade: 11 February 2019 - 05:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to sort emails in a string?

#2 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 954
  • View blog
  • Posts: 3,783
  • Joined: 13-June 14

Re: How to sort emails in a string?

Posted 11 February 2019 - 05:36 AM

View Postbluemarmalade, on 11 February 2019 - 12:00 PM, said:

I'm thinking this can be done using a hashtable, where you can only have one of each item


If you need to keep the items in order, a hash table won't do, since in that kind of data structure the ordering is sacrificed for the constant time performance. Does C# have a sorted set? That would allow you to store unique items only and keep them in sorted order (probably using a binary search tree under the hood).
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is offline

  • awks lol ffs
  • member icon

Reputation: 6693
  • View blog
  • Posts: 27,471
  • Joined: 12-December 12

Re: How to sort emails in a string?

Posted 11 February 2019 - 05:48 AM

If you just want to discard duplicates you can use LINQ Distinct(). Similarly you can use OrderBy to sort them.
Was This Post Helpful? 2
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6717
  • View blog
  • Posts: 22,931
  • Joined: 05-May 12

Re: How to sort emails in a string?

Posted 11 February 2019 - 05:54 AM

As a quick aside, you cannot simply split on commas because commas are valid inside the local part of an email addresses as long as they are appropriately wrapped in quotes as per the RFCs.

Anyway, simply use LINQ to sort and filter (OrderBy and Distinct).

Looks like Andrew beat me to the LINQ approach.
Was This Post Helpful? 2
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6717
  • View blog
  • Posts: 22,931
  • Joined: 05-May 12

Re: How to sort emails in a string?

Posted 11 February 2019 - 06:04 AM

There is a SortedSet<T>.
Was This Post Helpful? 0
  • +
  • -

#6 bluemarmalade   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 05-July 15

Re: How to sort emails in a string?

Posted 11 February 2019 - 07:43 AM

I've never seen an emailadress with a comma in it. I didn't know that, would such an adress screw with my sort?

Thanks for the useful replies! I will try with linq. But this works, using hashset. If there are duplicates, the add will skip it:

        public static List<string> EmailstringToList(string emailstring)
        {
            var emails = new List<string>();
            if (string.IsNullOrWhiteSpace(emailstring))
            {
                return emails;
            }
            string[] separatingChars = { "\r\n", "," };
            emails = emailstring.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries)
            .Select(x => x.Trim()).ToList();
            var emailCheck = new EmailAddressAttribute();
            var newEmails = new HashSet<string>();
            foreach (var item in emails)
            {
                if (emailCheck.IsValid(item))
                {
                    newEmails.Add(item);
                    //emails.Remove(item);
                }
            }
            return newEmails.ToList();
        }


This post has been edited by bluemarmalade: 11 February 2019 - 07:44 AM

Was This Post Helpful? 0
  • +
  • -

#7 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2730
  • View blog
  • Posts: 10,898
  • Joined: 03-December 12

Re: How to sort emails in a string?

Posted 11 February 2019 - 09:26 AM

I'm curious how you get these email addresses as well? But Linq is the easiest route to take, as was already mentioned.
Was This Post Helpful? 0
  • +
  • -

#8 mjosh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-February 19

Re: How to sort emails in a string?

Posted 11 February 2019 - 09:53 AM

I also want to know about the sort emails in string.
Was This Post Helpful? 0
  • +
  • -

#9 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2730
  • View blog
  • Posts: 10,898
  • Joined: 03-December 12

Re: How to sort emails in a string?

Posted 11 February 2019 - 10:10 AM

Linq


		string emails = "g.example.com, a.example.com\r\n an.example.com\r\n ab.example.com,";
		
		string[] separatingChars = { "\r\n", "," };
		var sort = from s in emails.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries)
	orderby s select s;
		foreach (string c in sort)
		{
			Console.WriteLine("{0}", c.Trim());
		}
		

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6717
  • View blog
  • Posts: 22,931
  • Joined: 05-May 12

Re: How to sort emails in a string?

Posted 11 February 2019 - 01:55 PM

View Postbluemarmalade, on 11 February 2019 - 09:43 AM, said:

I've never seen an emailadress with a comma in it.

Check it out:
var emailAttr = new EmailAddressAttribute();
string email = "\"Bond,James\"@mi6.gov.uk";
Console.WriteLine(emailAttr.IsValid(email));



View Postbluemarmalade, on 11 February 2019 - 09:43 AM, said:

I didn't know that, would such an adress screw with my sort?

Yes, because you would be sorting the wrong data.
The following:
        static void Main()
        {
            void ShowValid(IEnumerable<string> addresses)
            {
                var emailAttr = new EmailAddressAttribute();
                foreach (var email in addresses)
                    Console.WriteLine("{0}: {1}", email, emailAttr.IsValid(email));
            }

            string[] validAddresses =
            {
                "[email protected]",
                "\"Bond,James\"@mi6.gov.uk",
                "[email protected]",
            };

            ShowValid(validAddresses);

            var emails = string.Join(", ", validAddresses);
            Console.WriteLine($"emails: {emails}");

            var invalidAddresses = emails.Split(new string [] { ",", "\r\n" },
                                                StringSplitOptions.RemoveEmptyEntries);
            ShowValid(invalidAddresses.Select(s => s.Trim()));
        }



Produces this output:
[email protected]: True
"Bond,James"@mi6.gov.uk: True
[email protected]: True
emails: [email protected], "Bond,James"@mi6.gov.uk, [email protected]
[email protected]: True
"Bond: False
James"@mi6.gov.uk: True
[email protected]: True


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1