DateTimeComparer.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2005, Mathew Hall
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. */
  30. using System;
  31. using System.Collections;
  32. using System.Windows.Forms;
  33. using XPTable.Models;
  34. namespace XPTable.Sorting
  35. {
  36. /// <summary>
  37. /// An IComparer for sorting Cells that contain DateTime information
  38. /// </summary>
  39. public class DateTimeComparer : ComparerBase
  40. {
  41. #region Constructor
  42. /// <summary>
  43. /// Initializes a new instance of the DateTimeComparer class with the specified
  44. /// TableModel, Column index and SortOrder
  45. /// </summary>
  46. /// <param name="tableModel">The TableModel that contains the data to be sorted</param>
  47. /// <param name="column">The index of the Column to be sorted</param>
  48. /// <param name="sortOrder">Specifies how the Column is to be sorted</param>
  49. public DateTimeComparer(TableModel tableModel, int column, SortOrder sortOrder) : base(tableModel, column, sortOrder)
  50. {
  51. }
  52. #endregion
  53. #region Methods
  54. /// <summary>
  55. /// Compares two objects and returns a value indicating whether one is less
  56. /// than, equal to or greater than the other
  57. /// </summary>
  58. /// <param name="a">First object to compare</param>
  59. /// <param name="b">Second object to compare</param>
  60. /// <returns>-1 if a is less than b, 1 if a is greater than b, or 0 if a equals b</returns>
  61. public override int Compare(object a, object b)
  62. {
  63. Cell cell1 = (Cell) a;
  64. Cell cell2 = (Cell) b;
  65. // check for null cells
  66. if (cell1 == null && cell2 == null)
  67. {
  68. return 0;
  69. }
  70. else if (cell1 == null)
  71. {
  72. return -1;
  73. }
  74. else if (cell2 == null)
  75. {
  76. return 1;
  77. }
  78. // check for null data
  79. if (cell1.Data == null && cell2.Data == null)
  80. {
  81. return 0;
  82. }
  83. else if (cell1.Data == null)
  84. {
  85. return -1;
  86. }
  87. else if (cell2.Data == null)
  88. {
  89. return 1;
  90. }
  91. return Convert.ToDateTime(cell1.Data).CompareTo(Convert.ToDateTime(cell2.Data));
  92. }
  93. #endregion
  94. }
  95. }