none
How do I sort contants of my txt file? RRS feed

  • Question

  • So I have text file like this:

    product1:info1:12
    product2:info2:53
    product3:info3:2
    product4:info4:1

    How do I sort them by the price (third attrubite)

    Monday, May 6, 2019 5:16 PM

Answers

  • The basics (as what Alberto mentioned)

    Class to hold data, note the override of ToString.

    public class Item
    {
        public string Name  { get; set; }
        public string Data { get; set; }
        public int Price { get; set; }
        public override string ToString()
        {
            return $"{Name}:{Data}:{Price}";
        }
    }

    Now imagine the list below was read from a file, the next line sorts the list by price in descending order.

    var itemList = new List<Item>()
    {
        new Item() {Name = "Product1", Data = "Info1", Price = 12},
        new Item() {Name = "Product2", Data = "Info2", Price = 53},
        new Item() {Name = "Product3", Data = "Info3", Price = 2},
        new Item() {Name = "Product4", Data = "Info4", Price = 1}
    };
    
    File.WriteAllLines("abc.txt", itemList
        .OrderByDescending(item => item.Price).Select(item => item.ToString()));

    Open the file and we get the following

    Product2:Info2:53
    Product1:Info1:12
    Product3:Info3:2
    Product4:Info4:1


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, May 6, 2019 7:03 PM
    Moderator

All replies

  • The trick is that you don't sort your file. Instead, you read the file into a data structure in memory (such as a List<MyClass>), then sort the data in memory, and then write back the data into the file.

    So, to answer your question without having to write lots of code, where you probably already know how to write many of the parts, it would be useful to know what you know and what you don't know. ¿Do you know how to read the contents of a file? ¿Do you know how to parse each line? ¿Do you know how to define a class to contain the parsed data? ¿Do you know how to load the parsed data into a list of objects of the class? ¿Do you know how to sort a list in memory? ¿Do you know how to write it back to the file?


    Monday, May 6, 2019 5:30 PM
    Moderator
  • I know how to read contents of a file and I know how to write them back to file

    Monday, May 6, 2019 5:49 PM
  • The basics (as what Alberto mentioned)

    Class to hold data, note the override of ToString.

    public class Item
    {
        public string Name  { get; set; }
        public string Data { get; set; }
        public int Price { get; set; }
        public override string ToString()
        {
            return $"{Name}:{Data}:{Price}";
        }
    }

    Now imagine the list below was read from a file, the next line sorts the list by price in descending order.

    var itemList = new List<Item>()
    {
        new Item() {Name = "Product1", Data = "Info1", Price = 12},
        new Item() {Name = "Product2", Data = "Info2", Price = 53},
        new Item() {Name = "Product3", Data = "Info3", Price = 2},
        new Item() {Name = "Product4", Data = "Info4", Price = 1}
    };
    
    File.WriteAllLines("abc.txt", itemList
        .OrderByDescending(item => item.Price).Select(item => item.ToString()));

    Open the file and we get the following

    Product2:Info2:53
    Product1:Info1:12
    Product3:Info3:2
    Product4:Info4:1


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, May 6, 2019 7:03 PM
    Moderator