Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to ignore -ve value while sorting generic list in c#.

How to ignore -ve value while sorting generic list in c#.

Scheduled Pinned Locked Moved ASP.NET
csharpalgorithmsdata-structurestutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rahul net11
    wrote on last edited by
    #1

    Hi all, I want to ignore negative while sorting a generic array list. eg. If list contain following values 0.3 0.2 -0.3 after sofrting it should values should be in following order 0.3 -0.3 0.2 Any solution guyz. Thanks in advance.

    People Laugh on me Because i am Different but i Laugh on them Because they all are same.

    P C 2 Replies Last reply
    0
    • R rahul net11

      Hi all, I want to ignore negative while sorting a generic array list. eg. If list contain following values 0.3 0.2 -0.3 after sofrting it should values should be in following order 0.3 -0.3 0.2 Any solution guyz. Thanks in advance.

      People Laugh on me Because i am Different but i Laugh on them Because they all are same.

      P Offline
      P Offline
      Prosanta Kundu online
      wrote on last edited by
      #2

      Write a class that implements IComparer.

      public class myComparer : IComparer
      {
      public Boolean Descending;
      #region IComparer Members

      public int Compare(object x, object y)
      {
          Double obj1   = (Double)x;
          if (obj1 < 0)  obj1 = -(obj1);
      
          Double obj2  = (Double)y;
          if (obj2 < 0)  obj2 = -(obj2);
      
          if (Descending )
          {
              if (obj1 < obj2 )
                  return 1;
              if (obj1 > obj2)
                  return -1;
          }
          else
          {
             if (obj1 > obj2 )
                  return 1;
              if (obj1< obj2)
                  return -1;
          }
          return 0;
      }
      
      #endregion
      

      }

      Pass a object of your custom class in ArrayList.sort method

      ArrayList arr = new ArrayList();
      arr.Add(0.3);
      arr.Add(0.2);
      arr.Add(-0.3);

      myComparer objComparer = new myComparer();
      objComparer.Descending = true;
      arr.Sort(objComparer);

      modified on Monday, August 30, 2010 5:31 AM

      1 Reply Last reply
      0
      • R rahul net11

        Hi all, I want to ignore negative while sorting a generic array list. eg. If list contain following values 0.3 0.2 -0.3 after sofrting it should values should be in following order 0.3 -0.3 0.2 Any solution guyz. Thanks in advance.

        People Laugh on me Because i am Different but i Laugh on them Because they all are same.

        C Offline
        C Offline
        cramteke
        wrote on last edited by
        #3

        you can do some thing like this

        public partial class _Default : System.Web.UI.Page
        {
        protected void Page_Load(object sender, EventArgs e)
        {
        List<decimal> lstIntCol = new List<decimal>();
        lstIntCol.Add(0.3M);
        lstIntCol.Add(0.2M);
        lstIntCol.Add(-0.3M);
        lstIntCol.Sort(new decimalComparer());
        foreach (decimal d in lstIntCol)
        {
        Response.Write(d.ToString() + "--");
        }
        }
        }

        public class decimalComparer : System.Collections.Generic.IComparer<decimal>
        {

        #region IComparer<decimal> Members
        
        public int Compare(decimal x, decimal y)
        {
            return decimal.Compare(Math.Abs(y),Math.Abs(x));
        }
        
        #endregion
        

        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups