FileNameSuffixComparer.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace Dfs.WayneChina.FairbanksRTData.Support
  6. {
  7. public class FileNameSuffixComparer : IComparer<string>
  8. {
  9. #region Logger
  10. NLog.Logger logger = NLog.LogManager.LoadConfiguration("NLog.config").GetLogger("Fairbanks");
  11. #endregion
  12. public int Compare(string x1, string y1)
  13. {
  14. var x = Path.GetFileNameWithoutExtension(x1);
  15. var y = Path.GetFileNameWithoutExtension(y1);
  16. logger.Debug(string.Format("\tComparing {0} and {1}", x, y));
  17. if (!x.Contains('[') || !y.Contains('['))
  18. {
  19. if (x.Length > y.Length)
  20. {
  21. logger.Debug("\treturn 1");
  22. return 1;
  23. }
  24. if (x.Length < y.Length)
  25. {
  26. logger.Debug("\treturn -1");
  27. return -1;
  28. }
  29. logger.Debug("\treturn 0");
  30. return 0;
  31. }
  32. int xNumber = GetSuffixNumber(x);
  33. int yNumber = GetSuffixNumber(y);
  34. if (xNumber > yNumber)
  35. return 1;
  36. if (xNumber < yNumber)
  37. return -1;
  38. return 0;
  39. }
  40. private int GetSuffixNumber(string x)
  41. {
  42. int xStartIndex = x.IndexOf('[');
  43. int xEndIndex = x.IndexOf(']');
  44. int xNumber = Convert.ToInt32(x.Substring(xStartIndex + 1, xEndIndex - xStartIndex - 1));
  45. return xNumber;
  46. }
  47. }
  48. }