File

projects/wms-framework/src/lib/baseframework/SortDescription.ts

Description

Defines the direction and the property name to be used as the criteria for sorting a collection.

Index

Properties
Methods
Accessors

Constructor

constructor(arg1?: unknown, arg2?: unknown)
Parameters :
Name Type Optional
arg1 unknown Yes
arg2 unknown Yes

Properties

Private _direction
Type : ListSortDirection
Default value : ListSortDirection.Ascending
Private _propertyName
Type : string
Default value : null
Private _sealed
Type : boolean
Default value : false

Methods

Static sortFunction
sortFunction(sortDescriptions: Iterable<SortDescription>, x: any, y: any)

Provides a comparison function that uses the given {SortDescription} objects as propertie to compare (It is not case sensitive).

to compare x to y.

Parameters :
Name Type Optional Description
sortDescriptions Iterable<SortDescription> No

the list of {SortDescription} objects used as properties to compare x to y.

x any No

the first element to compare

y any No

the second element to compare

Returns : number

{number} -1 if x lower than y, 1 if x bigger than y, otherwise 0

Accessors

Direction
getDirection()

Gets the Direction property

Returns : ListSortDirection
setDirection(direction: ListSortDirection)

Sets the Direction property

Parameters :
Name Type Optional
direction ListSortDirection No
Returns : void
PropertyName
getPropertyName()

Gets the PropertyName property

Returns : string
setPropertyName(propertyName: string)

Sets the PropertyName property

Parameters :
Name Type Optional
propertyName string No
Returns : void
IsSealed
getIsSealed()

Gets the Sealed property

Returns : boolean
import { ListSortDirection } from './ListSortDirection';
import { ArgumentException } from './Exceptions';

/**
 * Defines the direction and the property name to be used as the criteria for sorting a collection.
 *
 * @export
 * @class SortDescription
 * @wType System.ComponentModel.SortDescription
 */
export class SortDescription {
  private _direction: ListSortDirection = ListSortDirection.Ascending;
  private _sealed: boolean = false;
  private _propertyName: string = null;
  /**
   * Creates an instance of SortDescription.
   * @param {*} length
   * @memberof SortDescription
   */
  constructor();
  constructor(propertyName: string, direction: ListSortDirection);
  constructor(arg1?: unknown, arg2?: unknown) {
    if (typeof arg1 == 'string' && typeof arg2 != 'undefined') {
      let propertyName = arg1 as string;
      let direction = arg2 as ListSortDirection;
      if (
        direction < ListSortDirection.Ascending ||
        direction > ListSortDirection.Descending
      )
        throw new ArgumentException('direction');
      this._propertyName = propertyName;
      this._direction = direction;
      this._sealed = false;
    }
  }
  /**
   *Gets the Direction property
   *
   * @type {ListSortDirection}
   * @memberof SortDescription
   */
  public get Direction(): ListSortDirection {
    return this._direction;
  }
  /**
   *Sets the Direction property
   *
   * @memberof SortDescription
   */
  public set Direction(direction: ListSortDirection) {
    this._direction = direction;
  }
  /**
   *Gets the PropertyName property
   *
   * @type {string}
   * @memberof SortDescription
   */
  public get PropertyName(): string {
    return this._propertyName;
  }
  /**
   *Sets the PropertyName property
   *
   * @memberof SortDescription
   */
  public set PropertyName(propertyName: string) {
    this._propertyName = propertyName;
  }
  /**
   *Gets the Sealed property
   *
   * @readonly
   * @type {boolean}
   * @memberof SortDescription
   */
  public get IsSealed(): boolean {
    return this._sealed;
  }

  /**
   * Provides a comparison function that uses the given {SortDescription} objects as propertie to compare (It is not case sensitive).
   *
   * @static
   * @param {Iterable<SortDescription>} sortDescriptions the list of {SortDescription} objects used as properties
   * to compare x to y.
   * @param {*} x the first element to compare
   * @param {*} y the second element to compare
   * @return {*}  {number} -1 if x lower than y, 1 if x bigger than y, otherwise 0
   * @memberof SortDescription
   */
  public static sortFunction(
    sortDescriptions: Iterable<SortDescription>,
    x: any,
    y: any
  ): number {
    let result = 0;
    for (const sortDescriptor of sortDescriptions) {
      const properties = sortDescriptor.PropertyName.split('.');
      const leftValue = properties.reduce((o, i) => o[i], x);
      const rightValue = properties.reduce((o, i) => o[i], y);
      if (leftValue == null && rightValue == null) {
        return 0;
      }

      if (
        leftValue == null ||
        leftValue.toString().toLowerCase() <
          rightValue?.toString().toLowerCase()
      ) {
        result =
          sortDescriptor.Direction === ListSortDirection.Ascending ? -1 : 1;
        break;
      } else if (
        rightValue == null ||
        leftValue?.toString().toLowerCase() >
          rightValue.toString().toLowerCase()
      ) {
        result =
          sortDescriptor.Direction === ListSortDirection.Ascending ? 1 : -1;
        break;
      }
    }
    return result;
  }
}

result-matching ""

    No results matching ""